{" Person" :{"年龄" :" 2","位置" :" san jose"}}
假设我有上面的JSON字符串,我很难理解如何从JSON中解析出Location。使用ObjectMapper,有没有办法(在Java中)?
答案 0 :(得分:0)
仅供记录,使用the Jackson tree model非常简单。这是一个例子:
public class JacksonTree {
public static final String JSON = "{ \"Person\" : { \"age\" : \"2\", " +
"\"Location\" : \"san jose\"} }";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readTree(JSON)
.get("Person").get("Location").asText());
}
}
输出:
san jose
答案 1 :(得分:0)
您可以将其解析为获取值的对象。实施例
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonParser {
public static void main(String[] args) {
String json = "{ \"Person\" : { \"age\" : \"2\", \"Location\" : \"san jose\"} }";
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode actualObj = mapper.readTree(json);
System.out.println(actualObj.findValue("Location"));
} catch (IOException e) {
e.printStackTrace();
}
}
}