对象到普通旧java对象的JSON数组
这是我第一次用Java进行外部api调用,所以请耐心等待,因为我不是很有经验。我得到了http请求并得到了响应,但现在我需要解析它 我正在尝试将json数组转换为java对象。我理解它的要点,但我看到的所有例子都不适用于我的问题 我需要json字符串中的“实体”对象。细节(也是一个数组)可以包含任何键/值对,所以我想把它放在每个实体对象的hashmap中。我尝试过gson库,但我找不到任何比一维json数组更深入的gson示例 我意识到这是一个广泛的问题,我不希望有人给我提供一个工作解决方案,但是一些提示或链接到相关指南将有很大的帮助对象到普通旧java对象的JSON数组,java,json,gson,pojo,Java,Json,Gson,Pojo,这是我第一次用Java进行外部api调用,所以请耐心等待,因为我不是很有经验。我得到了http请求并得到了响应,但现在我需要解析它 我正在尝试将json数组转换为java对象。我理解它的要点,但我看到的所有例子都不适用于我的问题 我需要json字符串中的“实体”对象。细节(也是一个数组)可以包含任何键/值对,所以我想把它放在每个实体对象的hashmap中。我尝试过gson库,但我找不到任何比一维json数组更深入的gson示例 我意识到这是一个广泛的问题,我不希望有人给我提供一个工作解决方案,但
有许多json序列化/反序列化框架可用。我建议你看一看 基本上,您必须创建对应于
json
schema的模型,并将json
反序列化到对象中。根据问题中的示例,模型如下所示:
@JsonIgnoreProperties(ignoreUnknown = true)
class Response {
@JsonProperty("return")
private ResponseObject responseObject;
public ResponseObject getResponseObject() {
return responseObject;
}
public void setResponseObject(ResponseObject responseObject) {
this.responseObject = responseObject;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
class ResponseObject {
private List<Entity> entities;
public List<Entity> getEntities() {
return entities;
}
public void setEntities(List<Entity> entities) {
this.entities = entities;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Entity {
private String id;
private List<Details> details;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Details> getDetails() {
return details;
}
public void setDetails(List<Details> details) {
this.details = details;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Details {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Detail{
private String name;
private String value;
private boolean match;
// + getters / setters
}
public class Entity{
private int id;
private List<Detail> details;
private String link;
private List<String> proofs;
// you don't have any example data for this, so I'm assuming strings
// + getters / setters
}
public class Result{
private List<Entity> entities;
private String notice;
// + getters / setters
}
这是答案。如果我是你,我会使用,而不是GSON。它专门研究JavaBeans风格的映射。编写如下类:
@JsonIgnoreProperties(ignoreUnknown = true)
class Response {
@JsonProperty("return")
private ResponseObject responseObject;
public ResponseObject getResponseObject() {
return responseObject;
}
public void setResponseObject(ResponseObject responseObject) {
this.responseObject = responseObject;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
class ResponseObject {
private List<Entity> entities;
public List<Entity> getEntities() {
return entities;
}
public void setEntities(List<Entity> entities) {
this.entities = entities;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Entity {
private String id;
private List<Details> details;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Details> getDetails() {
return details;
}
public void setDetails(List<Details> details) {
this.details = details;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
class Details {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Detail{
private String name;
private String value;
private boolean match;
// + getters / setters
}
public class Entity{
private int id;
private List<Detail> details;
private String link;
private List<String> proofs;
// you don't have any example data for this, so I'm assuming strings
// + getters / setters
}
public class Result{
private List<Entity> entities;
private String notice;
// + getters / setters
}
正如我的stackoverflow用户同事之前所发布的,对于这种初始化,Jackson API会更好。不过,我已将您的问题的解决方案发布到Gson 我注意到您希望将详细信息存储为以id为键的HashMap。然而,看起来这个id实际上与实体相关,而不是与细节相关 免责声明,我变得懒惰并使用在线POJO生成器,因为我不想为所有Json元素创建对象;)它仍然展示了应该如何做到这一点:
class Main{
public static void main(String[] args) throws FileNotFoundException {
//this is just to load the json file
String input = new Scanner(new File("test.txt")).useDelimiter("\\Z").next();
System.out.println(input);
Gson gson = new Gson();
Example arr = gson.fromJson(input, Example.class);
System.out.println(arr);
}
public class Detail {
@SerializedName("name")
@Expose
public String name;
@SerializedName("value")
@Expose
public String value;
@SerializedName("match")
@Expose
public Boolean match;
@Override
public String toString() {
return "Detail [name=" + name + ", value=" + value + ", match=" + match + "]";
}
}
public class Entity {
@SerializedName("id")
@Expose
public Integer id;
@SerializedName("details")
@Expose
public List<Detail> details = null;
@SerializedName("proofs")
@Expose
public List<Object> proofs = null;
@SerializedName("link")
@Expose
public String link;
@Override
public String toString() {
return "Entity [id=" + id + ", details=" + details + ", proofs=" + proofs + ", link=" + link + "]";
}
}
public class Example {
@SerializedName("return")
@Expose
public Return _return;
@Override
public String toString() {
return "Example [_return=" + _return + "]";
}
}
public class Return {
@SerializedName("entities")
@Expose
public List<Entity> entities = null;
@SerializedName("notice")
@Expose
public String notice;
@Override
public String toString() {
return "Return [entities=" + entities + ", notice=" + notice + "]";
}
}
}
尽管有一些答案建议您使用Jackson,但您仍然可以通过Gson的默认配置轻松实现,只需在映射之间创建适当的关系即可:
//用参数化的泛型响应可以包含除原语以外的任何类型
最后的课堂反应{
@序列化名称(“返回”)
最终T ret=null;
}
最终课程实体和通知{
最终列表实体=空;
最终字符串通知=null;
}
最终类实体{
//与Object及其任何子类不同,`int`作为原语不能为null
//简单0也无法工作,因为编译器将内联它
//因此,返回一个已经包含0的值是一种欺骗javac
最终int id=整数。valueOf(0);
最终列表详细信息=空;
//您的JSON文档没有提供足够的元素类型信息
//因此,这取决于Gson如何解析JSON标记
最终列表证明=空;
最终URL链接=null;
}
最终课程详情{
最终字符串名称=null;
最终字符串值=null;
//对于基本布尔值或布尔值也一样。FALSE
最终布尔匹配=boolean.valueOf(false);
}
示例用法:
private static final String JSON=“{\'return\':{\'entities\':[{\'id\':2385,\'details\':[{\'name\':\'Other Known name\',\'value\':\'John Wick\',\'match\':false}],\'proof\':[],'link\':“http://domain.gg/users?id=2385\“},{\'id\':2384,\'details\':[{\'name\':\'Discord id\',\'value\':\'159985870458322944\',\'match\':false},{\'name\':\”SteamID64\“,”值\“:”76561197991558078\“,”匹配\“:真“,”名称\“:”蒸汽虚荣\“,”值\“:”测试\“,”匹配\“:假“,”名称\“:”PS4 \“,”值\“:”约翰U S \“,”匹配\“:假“,”名称\“:”XBox \“,”值\“:”约翰S \“,”匹配\“,”假“,”姓名\“,”电子邮件\“,”约翰\“,”值\”_smith@gmail.com\“,\”匹配\“:真},{\”名称\“:\”注释\“,\”值\“:\”测试用户\“,\”匹配\“:false},{\”名称\“:\”其他已知名称\“,\”值\“:\”乔纳森\“,\”匹配\“:false},{\”名称\“:\”Reddit\“,\”值\“:\”/u/johns\”,\”匹配\“:true}],”证据\“:[],”链接\“:”http://domain.gg/users?id=2384\“},{\'id\':1680,\'details\':[{\'name\':\'Other Known name\',\'value\':\'Johny\',\'match\':false},{\'name\':\'streamid64\',\'value\':\”76561198213003675\“,”匹配\“:真“}],”证明\“:[],”链接\“:\”http://domain.gg/users?id=1680\“},{id\':1689,\'details\':[{\'name\':\'Other Known name\',\'value\':\'johnypeto\',\'match\':false},{\'name\':'streamid64\',\'value\':\'76561198094228192\',\'match\':true},\'proof\':[],'link\':”http://domain.gg/users?id=1689\“}],\“通知\”:”显示4个匹配项中的4个。\“}”;
私有静态最终Gson Gson=new Gson();
私有静态最终TypeToken responseTypeToken=新的TypeToken(){
};
公共静态void main(最终字符串…参数){
最终响应Response=gson.fromJson(JSON,responseTypeToken.getType());
最终字符串值=response.ret.entities.get(1).details.get(3).value;
系统输出打印项次(值);
}
输出:
约翰·苏
“细节(也是一个数组)可以包含任何键/值对”。你确定这是真的吗?我希望任何半体面的API都遵守规范。否则你怎么知道会发生什么?如果我是对的,我会找到规范并创建一个Java对象来为您收到的响应建模。。。。但我找不到比一维json数组更深入的gson示例——所以你甚至没有尝试做一个小实验?看看这个问题:我认为它会有帮助。
class Main{
public static void main(String[] args) throws FileNotFoundException {
//this is just to load the json file
String input = new Scanner(new File("test.txt")).useDelimiter("\\Z").next();
System.out.println(input);
Gson gson = new Gson();
Example arr = gson.fromJson(input, Example.class);
System.out.println(arr);
}
public class Detail {
@SerializedName("name")
@Expose
public String name;
@SerializedName("value")
@Expose
public String value;
@SerializedName("match")
@Expose
public Boolean match;
@Override
public String toString() {
return "Detail [name=" + name + ", value=" + value + ", match=" + match + "]";
}
}
public class Entity {
@SerializedName("id")
@Expose
public Integer id;
@SerializedName("details")
@Expose
public List<Detail> details = null;
@SerializedName("proofs")
@Expose
public List<Object> proofs = null;
@SerializedName("link")
@Expose
public String link;
@Override
public String toString() {
return "Entity [id=" + id + ", details=" + details + ", proofs=" + proofs + ", link=" + link + "]";
}
}
public class Example {
@SerializedName("return")
@Expose
public Return _return;
@Override
public String toString() {
return "Example [_return=" + _return + "]";
}
}
public class Return {
@SerializedName("entities")
@Expose
public List<Entity> entities = null;
@SerializedName("notice")
@Expose
public String notice;
@Override
public String toString() {
return "Return [entities=" + entities + ", notice=" + notice + "]";
}
}
}
Example [_return=Return [entities=[Entity [id=2385, details=[Detail [name=Other Known Name, value=John Wick, match=false]], proofs=[], link=http://domain.gg/users?id=2385], Entity [id=2384, details=[Detail [name=Discord ID, value=159985870458322944, match=false], Detail [name=SteamID64, value=76561197991558078, match=true], Detail [name=SteamVanity, value=test, match=false], Detail [name=PS4, value=John_S, match=false], Detail [name=XBox, value=John S, match=false], Detail [name=Email, value=john_smith@gmail.com, match=true], Detail [name=Comment, value=Test user, match=false], Detail [name=Other Known Name, value=Jonathan, match=false], Detail [name=Reddit, value=/u/johns, match=true]], proofs=[], link=http://domain.gg/users?id=2384], Entity [id=1680, details=[Detail [name=Other Known Name, value=Johny, match=false], Detail [name=SteamID64, value=76561198213003675, match=true]], proofs=[], link=http://domain.gg/users?id=1680], Entity [id=1689, details=[Detail [name=Other Known Name, value=JohnnyPeto, match=false], Detail [name=SteamID64, value=76561198094228192, match=true]], proofs=[], link=http://domain.gg/users?id=1689]], notice=Showing 4 out of 4 matches.]]