gpt4 book ai didi

java - 如何将json响应设置为java对象

转载 作者:行者123 更新时间:2023-12-01 09:16:57 25 4
gpt4 key购买 nike

我有 api 并尝试访问链接:

server/apps/ProcessRequest?req={"agent_id":"id","proccode":"380000","agent_pass":"pass","req_datetime":"20161108132741","customer_id":"547201743150","product_code":"001002","rrn":"uniquecode"}

我的问题是上面链接的响应,我想将响应设置为java对象并使用它。

我得到这个回复:

{
"resp_code": "0000",
"amount": "0",
"data": {
"Status": "0000",
"NomorPelanggan": "547",
"IDPelanggan": "547",
"Unsold": "[]",
"Nama": "PLGN LPB., 05134710274",
"TeleponUP": "123",
"Daya": "2200",
"KodeUP": "54720",
"MaksimalKWH": "5000",
"Tagihan": [
{
"Periode": "201611",
"Total": 0
}
],
"NomorMeter": "05134710274",
"Tarif": "R3",
"TotalTagihan": "0",
"ErrorMessage": "",
"SessionId": "2016110813321820497",
"Nominal": [
20000,
50000,
100000,
200000,
500000,
1000000,
5000000,
10000000,
50000000
],
"KodeDistribusi": "54"
},
"resp_desc": "Successful",
"proccode": "380000"}

如何将此响应设置为java对象?

该 api 不属于我,我现在不知道该 api 的 pojo 模型。

最佳答案

我认为你追求的是连载。如果您事先知道要接收的 Json 的格式,则可以简单地使用 Gson 解析 Json 并从中创建 Java 对象。

假设你有这个 json:

{
"type": "Fiat",
"model": "500",
"colour": "white"
}

尝试这样的事情:

public class Car {
private String type;
private String model;
private String colour;
// Getters and setters.
}

public static void main(final String... args) {
String json = getTheJsonSomehow();
Gson gson = new Gson();
Car car = gson.fromJson(json, Car.class);
}

参见the documentation

关于java - 如何将json响应设置为java对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480612/

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