gpt4 book ai didi

java - 从 JSON 读取数据

转载 作者:行者123 更新时间:2023-12-02 03:49:18 25 4
gpt4 key购买 nike

这是我的 JSON 响应

{
"data": [
{
"id": "txn_17pHAkI4mX5ntfze4If5wIxW",
"source": "tr_17pHAkI4mX5ntfzeIfNucubD",
"amount": -100000,
"currency": "usd",


},
{
"id": "txn_17pH21I4mX5ntfzesrhdZwyf",
"source": "tr_17pH21I4mX5ntfzeKLd0SWw0",
"amount": -100000,

},
{
"id": "txn_17pGVRI4mX5ntfzeBQPCWZZg",
"source": "tr_17pGVRI4mX5ntfzegJNe1r4o",
"amount": -100000,
"currency": "usd"
}
],

},
"url": "/v1/balance/history",
"count": null
}

我想从这三个列表中获取“金额”和“id”。如何用java实现呢?

最佳答案

查看Google's Gson .

您需要创建一个表示 json 数据的类。

class Response {
class Data {
public String id;
public String source;
public int amount;
public String currency;
}
public Data[] data;
public String url;
public int count;
}

然后将数据读入变量。

String json = "your json here";
Gson gson = new Gson();
Response response = gson.fromJson(json, Response.class);
for(Data d : response.data) {
System.out.println(d.id);
System.out.println(d.amount);
}

关于java - 从 JSON 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36013347/

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