将Json转换为Java Object时遇到问题。
我的“ jsonText”字段将json作为值,我希望将其放置在String中。我的自定义类具有以下结构。
Class Custom{
@JsonProperty(value = "field1")
private String field1;
@JsonProperty(value = "jsonText")
private String jsonText;
}
下面是我的代码:
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(inputString);
String nodeTree = node.path("jsonText").toString();
List<PatientMeasure> measuresList =mapper.readValue(nodeTree,
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, CustomClass.class) );
要转换的Json是:
"field1" : "000000000E",
"jsonText" : {
"rank" : "17",
"status" : "",
"id" : 0
}
异常得到了:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.StringReader@3362f02f; line: 1, column: 108] (through reference chain: com.Custom["jsonText"])
最佳答案
您可以尝试以下方法:
JSONArray ar= new JSONArray(result);
JSONObject jsonObj= ar.getJSONObject(0);
String strname = jsonObj.getString("NeededString");