我在将 Json 转换为 Java 对象时遇到问题。 我的“jsonText”字段有 json 作为值,我想将其放置在字符串中。我的自定义类具有以下结构。
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");
关于java - 如何使用 ObjectMapper 将 JSON 值视为 Java 中的字符串对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41380877/