![](/img/trans.png)
[英]Changing json libs to deserialize json array from com.fasterxml.jackson to org.codehaus.jackson?
[英]Parse Json with com.fasterxml.jackson instead of org.json
我想知道是否可以使用杰克逊库执行此精确操作。
String repo = response.toString();
JSONObject json = new JSONObject (repo);
String nameOfUser = json.getJSONObject(facebookID).getString("name");
谢谢,
是。 就像是:
ObjectMapper mapper = new ObjectMapper(); // reuse, usually static final
JsonNode ob = mapper.readTree(response.toString()); // or from File, URL, InputStream, Reader
String nameOfUser = ob.path(facebookID).path("name").asText();
// note: '.get()' also works, but returns nulls, 'path()' safer
尽管更方便的访问通常是使用JSON指针表达式完成的,例如:
String name = ob.at("/person/id").asText();
但我认为facebookID
是其他来源的ID。
更新:根据下面的评论,您想要的结构实际上可能是POJO,例如:
public class Response {
public User facebookID;
}
public class User {
public String id;
public String email;
public String first_name;
// ... and so forth: fields and/or getter+setter
}
然后您可以像这样直接绑定到类中:
Response resp = mapper.readValue(response.toString(), Response.class);
String name = resp.facebookID.name;
因此,与杰克逊合作的方法不止一种。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.