Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将这个嵌套的json字符串转换为java对象?_Java_Json - Fatal编程技术网

如何将这个嵌套的json字符串转换为java对象?

如何将这个嵌套的json字符串转换为java对象?,java,json,Java,Json,我有一个“特殊”格式的JSON字符串。 我想将其转换为java中的对象,但我不知道如何访问单个值,例如location或“resolved_at”的值 我试过使用GSON和JSONPOBJECT,但是这个不起作用 { "result": { "upon_approval": "proceed", "location": { "link": "https://instance.service- now.com/api/now/table/cmn_location

我有一个“特殊”格式的JSON字符串。 我想将其转换为java中的对象,但我不知道如何访问单个值,例如location或“resolved_at”的值

我试过使用GSON和JSONPOBJECT,但是这个不起作用

 {
  "result": {
    "upon_approval": "proceed",
    "location": {
      "link": "https://instance.service-  now.com/api/now/table/cmn_location/108752c8c611227501d4ab0e392ba97f",
      "value": "108752c8c611227501d4ab0e392ba97f"
    },
    "expected_start": "",
    "reopen_count": "",
    "sys_domain": {
      "link": "https://instance.service-  now.com/api/now/table/sys_user_group/global",
      "value": "global"
    },
    "description": "",
    "activity_due": "2016-01-22 16:12:37",
    "sys_created_by": "glide.maint",
    "resolved_at": "",
    "assigned_to": {
      "link": "https://instance.service-  now.com/api/now/table/sys_user/681b365ec0a80164000fb0b05854a0cd",
      "value": "681b365ec0a80164000fb0b05854a0cd"
    },
    "business_stc": "",
    "wf_activity": "",
    "sys_domain_path": "/",
    "cmdb_ci": {
      "link": "https://instance.service-  now.com/api/now/table/cmdb_ci/281190e3c0a8000b003f593aa3f20ca6",
      "value": "281190e3c0a8000b003f593aa3f20ca6"
    },
    "opened_by": {
      "link": "https://instance.service-  now.com/api/now/table/sys_user/glide.maint",
      "value": "glide.maint"
    },
    "subcategory": "",
    "comments": ""
  }
}

只需创建一个对象并使用objectmapper,如:

Myclass myclass = objectMapper.readValue(json, Myclass.class);
但是根据你的json是无效的

删除错误后,使用联机转换器创建类:

    -----------------------------------com.example.AssignedTo.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AssignedTo {

@SerializedName("link")
@Expose
private String link;
@SerializedName("value")
@Expose
private String value;

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
-----------------------------------com.example.CmdbCi.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class CmdbCi {

@SerializedName("link")
@Expose
private String link;
@SerializedName("value")
@Expose
private String value;

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("result")
@Expose
private Result result;

public Result getResult() {
return result;
}

public void setResult(Result result) {
this.result = result;
}

}
-----------------------------------com.example.Location.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Location {

@SerializedName("link")
@Expose
private String link;
@SerializedName("value")
@Expose
private String value;

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
-----------------------------------com.example.OpenedBy.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class OpenedBy {

@SerializedName("link")
@Expose
private String link;
@SerializedName("value")
@Expose
private String value;

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
-----------------------------------com.example.Result.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

@SerializedName("upon_approval")
@Expose
private String uponApproval;
@SerializedName("location")
@Expose
private Location location;
@SerializedName("expected_start")
@Expose
private String expectedStart;
@SerializedName("reopen_count")
@Expose
private String reopenCount;
@SerializedName("sys_domain")
@Expose
private SysDomain sysDomain;
@SerializedName("description")
@Expose
private String description;
@SerializedName("activity_due")
@Expose
private String activityDue;
@SerializedName("sys_created_by")
@Expose
private String sysCreatedBy;
@SerializedName("resolved_at")
@Expose
private String resolvedAt;
@SerializedName("assigned_to")
@Expose
private AssignedTo assignedTo;
@SerializedName("business_stc")
@Expose
private String businessStc;
@SerializedName("wf_activity")
@Expose
private String wfActivity;
@SerializedName("sys_domain_path")
@Expose
private String sysDomainPath;
@SerializedName("cmdb_ci")
@Expose
private CmdbCi cmdbCi;
@SerializedName("opened_by")
@Expose
private OpenedBy openedBy;
@SerializedName("subcategory")
@Expose
private String subcategory;
@SerializedName("comments")
@Expose
private String comments;

public String getUponApproval() {
return uponApproval;
}

public void setUponApproval(String uponApproval) {
this.uponApproval = uponApproval;
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
}

public String getExpectedStart() {
return expectedStart;
}

public void setExpectedStart(String expectedStart) {
this.expectedStart = expectedStart;
}

public String getReopenCount() {
return reopenCount;
}

public void setReopenCount(String reopenCount) {
this.reopenCount = reopenCount;
}

public SysDomain getSysDomain() {
return sysDomain;
}

public void setSysDomain(SysDomain sysDomain) {
this.sysDomain = sysDomain;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getActivityDue() {
return activityDue;
}

public void setActivityDue(String activityDue) {
this.activityDue = activityDue;
}

public String getSysCreatedBy() {
return sysCreatedBy;
}

public void setSysCreatedBy(String sysCreatedBy) {
this.sysCreatedBy = sysCreatedBy;
}

public String getResolvedAt() {
return resolvedAt;
}

public void setResolvedAt(String resolvedAt) {
this.resolvedAt = resolvedAt;
}

public AssignedTo getAssignedTo() {
return assignedTo;
}

public void setAssignedTo(AssignedTo assignedTo) {
this.assignedTo = assignedTo;
}

public String getBusinessStc() {
return businessStc;
}

public void setBusinessStc(String businessStc) {
this.businessStc = businessStc;
}

public String getWfActivity() {
return wfActivity;
}

public void setWfActivity(String wfActivity) {
this.wfActivity = wfActivity;
}

public String getSysDomainPath() {
return sysDomainPath;
}

public void setSysDomainPath(String sysDomainPath) {
this.sysDomainPath = sysDomainPath;
}

public CmdbCi getCmdbCi() {
return cmdbCi;
}

public void setCmdbCi(CmdbCi cmdbCi) {
this.cmdbCi = cmdbCi;
}

public OpenedBy getOpenedBy() {
return openedBy;
}

public void setOpenedBy(OpenedBy openedBy) {
this.openedBy = openedBy;
}

public String getSubcategory() {
return subcategory;
}

public void setSubcategory(String subcategory) {
this.subcategory = subcategory;
}

public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = comments;
}

}
-----------------------------------com.example.SysDomain.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class SysDomain {

@SerializedName("link")
@Expose
private String link;
@SerializedName("value")
@Expose
private String value;

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
编辑:如我所见,上面的代码在OOTB中不起作用。我已通过以下步骤使其工作:

  • 将@SerializedName替换为@JsonProperty
然后:


是什么让这个JSON“特别”?您可以使用jsonschema2pojo生成pojo。com@Henry我习惯了这种格式:{“第一”:“a”,“第二”:“b”}这里我们有{“结果”:{“第一”:“a”,“第二”:“b”}@AyoubRossi是的,但有什么区别呢?在这两种情况下,你都会做同样的事情——读取与每个键相关的值,评估它们的类型,覆盖该类型。如果你使用的是JSON反序列化库,那么大部分都已经为你处理了,你只需要提供一些关于类型的详细信息。我尝试过了,但我得到了null值而不是正确的值
ObjectMapper mapper = new ObjectMapper();
Example example = mapper.readValue(json, Example.class);