gpt4 book ai didi

java - json 到 java 对象(使用映射)?

转载 作者:行者123 更新时间:2023-12-01 14:51:16 26 4
gpt4 key购买 nike

 ObjectMapper mapper=new ObjectMapper();
String response1=client.execute(request1, responseHandler);
Map jsonObject=mapper.readValue(response1, Map.class);
jsonObject.get("docs");

我从字符串名称response1中的url获取json字符串并尝试映射它。我的response1字符串如下所示。

{"response":
{"numFound":5,
"start":0,
"docs":[
{"id":1},
{"id":2},
{"id":3},
{"id":4},
{"id":5}
]
}
}

我想从这个字符串中创建所有 id 的数组。我怎样才能做到这一点?我正在努力做jsonObject.get("docs") 但它没有做任何事情,它说(没有明确的返回值)

最佳答案

编辑
使用我的 GSON 示例中的数据模型。现在您可以通过以下方式做到这一点:

String response = "{\"response\":{\"numFound\":5,\"start\":0,\"docs\":[{\"id\":1},{\"id\":2},{\"id\":3},{\"id\":4},{\"id\":5}]}}";
ObjectMapper mapper = new ObjectMapper();
ObjectReader objectReader = mapper.reader(Body.class);
Body body = (Body) objectReader.readValue(response);
System.out.println(body.getResponse().getDocs());

GSON 示例
您应该使用 GSON到 JSON 解析。创建数据模型,帮助您处理“响应”字符串。看我的例子:

import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SourceCodeProgram {

public static void main(String[] args) throws Exception {
String response = "{\"response\":{\"numFound\":5,\"start\":0,\"docs\":[{\"id\":1},{\"id\":2},{\"id\":3},{\"id\":4},{\"id\":5}]}}";
Gson gson = new GsonBuilder().serializeNulls().create();

System.out.println("To JSON");
System.out.println(gson.toJson(createTestBody()));
System.out.println("From JSON");
System.out.println(gson.fromJson(response, Body.class));
}

private static Body createTestBody() {
Response response = new Response();
response.setNumFound(5);
response.setStart(0);
response.setDocs(Arrays.asList(new Doc(1), new Doc(2), new Doc(3),
new Doc(4), new Doc(5)));

Body body = new Body();
body.setResponse(response);
return body;
}
}

class Body {

private Response response;

public Response getResponse() {
return response;
}

public void setResponse(Response response) {
this.response = response;
}

@Override
public String toString() {
return "Body [response=" + response + "]";
}
}

class Response {

private int numFound;
private int start;
private List<Doc> docs;

public int getNumFound() {
return numFound;
}

public void setNumFound(int numFound) {
this.numFound = numFound;
}

public int getStart() {
return start;
}

public void setStart(int start) {
this.start = start;
}

public List<Doc> getDocs() {
return docs;
}

public void setDocs(List<Doc> docs) {
this.docs = docs;
}

@Override
public String toString() {
return "Response [numFound=" + numFound + ", start=" + start
+ ", docs=" + docs + "]";
}
}

class Doc {

private int id;

public Doc() {
}

public Doc(int id) {
this.id = id;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public String toString() {
return Integer.toString(id);
}
}

输出:

To JSON
{"response":{"numFound":5,"start":0,"docs":[{"id":1},{"id":2},{"id":3},{"id":4},{"id":5}]}}
From JSON
Body [response=Response [numFound=5, start=0, docs=[1, 2, 3, 4, 5]]]

您可以通过以下方式检索“文档”:

Body body = gson.fromJson(response, Body.class);
System.out.println(body.getResponse().getDocs());

关于java - json 到 java 对象(使用映射)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14817825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com