想要将 json 对象映射到 java 对象
want to map a json object unto a java object
我正在使用具有如下 json 输出的某个 API。我已将其解析为 String
。
json 输出为字符串:{"result":{"number":"INC0022500"}}
.
如您所见,它具有键 result
的嵌套对象。
我用来将上述 json 映射到对象的代码片段。
Gson gson = new Gson();
EspIncidentTrial staff = gson.fromJson(json, EspIncidentTrial.class);
ESPIncidentTrialclass:
import javax.persistence.Embeddable;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class EspIncidentTrial {
@JsonProperty("result")
private ResultTrial result;
public ResultTrial getResult() {
return result;
}
public void setResult(ResultTrial result) {
this.result = result;
}
}
我为嵌套对象创建了另一个 class ResultTrial
。下面是正文
结果试验class:
import javax.persistence.Embeddable;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultTrial {
@JsonProperty("number")
private String incidentId;
public String getIncidentId() {
return incidentId;
}
public void setIncidentId(String incidentId) {
this.incidentId = incidentId;
}
}
现在发生的事情是,在 EspIncidentTrial
class 中,对象 result
正在被映射。但是,在 ResultTrial
class 内部,没有进行任何映射。
我尝试将 json 对象中的键 result
视为字符串,但抛出了以下错误,这是预料之中的。
The error occured while parsing the JSON. com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 12 path $.result
请帮忙!
你在这里混合 gson
和 Jackson
。
您正在使用 Jackson
的注释,但使用 GSON's
方法对其进行反序列化。
使用Jackson's
objectMapper
反序列化。
例如:
ObjectMapper mapper = new ObjectMapper();
EspIncidentTrial staff = mapper.readValue(json, EspIncidentTrial.class);
你可以试试这个....
String searchdata="{\"userPojo\":{\"userid\":1156}}";
JSONObject jsonObject=new JSONObject(searchdata);
SummaryPojo summaryPojo=new SummaryPojo();
if(searchdata.contains("userPojo"))
{
字符串 jsonstring=jsonObject.getString("userPojo");
Gson gson = new Gson();
UserPojo userPojo = gson.fromJson(searchdata, UserPojo.class);
summaryPojo.setUserPojo(userPojo);
}
我正在使用具有如下 json 输出的某个 API。我已将其解析为 String
。
json 输出为字符串:{"result":{"number":"INC0022500"}}
.
如您所见,它具有键 result
的嵌套对象。
我用来将上述 json 映射到对象的代码片段。
Gson gson = new Gson();
EspIncidentTrial staff = gson.fromJson(json, EspIncidentTrial.class);
ESPIncidentTrialclass:
import javax.persistence.Embeddable;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class EspIncidentTrial {
@JsonProperty("result")
private ResultTrial result;
public ResultTrial getResult() {
return result;
}
public void setResult(ResultTrial result) {
this.result = result;
}
}
我为嵌套对象创建了另一个 class ResultTrial
。下面是正文
结果试验class:
import javax.persistence.Embeddable;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultTrial {
@JsonProperty("number")
private String incidentId;
public String getIncidentId() {
return incidentId;
}
public void setIncidentId(String incidentId) {
this.incidentId = incidentId;
}
}
现在发生的事情是,在 EspIncidentTrial
class 中,对象 result
正在被映射。但是,在 ResultTrial
class 内部,没有进行任何映射。
我尝试将 json 对象中的键 result
视为字符串,但抛出了以下错误,这是预料之中的。
The error occured while parsing the JSON. com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 12 path $.result
请帮忙!
你在这里混合 gson
和 Jackson
。
您正在使用 Jackson
的注释,但使用 GSON's
方法对其进行反序列化。
使用Jackson's
objectMapper
反序列化。
例如:
ObjectMapper mapper = new ObjectMapper();
EspIncidentTrial staff = mapper.readValue(json, EspIncidentTrial.class);
你可以试试这个....
String searchdata="{\"userPojo\":{\"userid\":1156}}";
JSONObject jsonObject=new JSONObject(searchdata);
SummaryPojo summaryPojo=new SummaryPojo();
if(searchdata.contains("userPojo"))
{
字符串 jsonstring=jsonObject.getString("userPojo");
Gson gson = new Gson();
UserPojo userPojo = gson.fromJson(searchdata, UserPojo.class);
summaryPojo.setUserPojo(userPojo);
}