gpt4 book ai didi

java - 无法从 JSON 对象获取特定数据

转载 作者:行者123 更新时间:2023-12-02 06:35:54 26 4
gpt4 key购买 nike

我正在尝试使用 org.json.JSONObject 库从 json 响应中提取特定数据这是我的 json 响应:

{
"facets": {
"application": [
{
"name": "38",
"distribution": 1
}
],
"node": [
{
"name": "frstlwardu03_05",
"distribution": 1
}
],
"area": [
{
"name": "x",
"distribution": 1
}
],
"company": [
{
"name": "war001",
"distribution": 1
}
]
},
"duObjects": [
{
"id": "TASK|TSK(ZRM760J)(000)(ZRM760JU00)(000)|ZSRPSRM000",
"name": "TSK(ZRM760J)(000)(ZRM760JU00)(000)",
"mu": "ZSRPSRM000",
"label": "",
"session": "ZRM760J|000",
"sessionLabel": "SAP SRM Achats frais generaux execution",
"uprocHeader": "ZRM760JU00|000",
"uprocHeaderLabel": "Header for SRM760J",
"uprocHeaderType": "CL_INT",
"domain": "M",
"domainLabel": "",
"application": "38",
"applicationLabel": "magasin",
"highlightResult": {
"name": "name",
"word": "TSK"
}
}
],
"totalCount": 1,
"pageSize": 10,
"pageCurrent": 1,
"pageNb": 1
}

这是我用来将 URL 调用转换为 jsonobject 的方法:

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException    
{
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-
8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}

当我调用此方法时,我能够获取 Duobject 中的数据:

public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("http://frstmwarwebsrv.orsyptst.com:9000/duobject?
searchString=TSK(ZRM760J)(000)(ZRM760JU00)
(000)&filterchecks=nameJob,nameWF,nameSWF,application,domain&p.index=0&p.size=10");
System.out.println(json.getJSONArray("duObjects"));


}

我是否可以只提取 DuObjects 的名称字段?

最佳答案

您可以使用

System.out.println(json.getJSONArray("duObjects").getJSONObject(0).getString("name"));

获取名称。

关于java - 无法从 JSON 对象获取特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19656209/

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