gpt4 book ai didi

android - 如何解析 JSON 对象以获取数组中的数组?

转载 作者:行者123 更新时间:2023-11-30 02:59:14 25 4
gpt4 key购买 nike

这是我的 JSON 输出。我正在尝试从说明中返回“id”:“9040”,但遇到了问题

{
"packages": [
{
"instructions": [
{
"id": "9040",
"fn": "test.xml",
"@type": "down",
"fp": ""
}
],
"id": "47968",
"time": 1396036698630,
"priority": 0
}

]

这是我返回“Package”->“Id”的代码

         JSONObject jObject = new JSONObject(json);
JSONArray jsonMainNode = jObject.optJSONArray("packages");
int lengthJsonArr = jsonMainNode.length();
for(int i=0; i < lengthJsonArr; i++)
{
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
fileid = Integer.parseInt(jsonChildNode.optString("id").toString());
if (fileid > 0 )

Log.i("JSON parse", "id = "+ fileid);
}

如有任何帮助,我们将不胜感激。

谢谢

最佳答案

你忘了 JSONArray 指令

    {
"packages": [
{
"instructions": [
{
"id": "9040",
"fn": "test.xml",
"@type": "down",
"fp": ""
}
],
"id": "47968",
"time": 1396036698630,
"priority": 0
}
]
}

解析

JSONObject jObject = new JSONObject(json);
JSONArray jsonMainNode = jObject.optJSONArray("packages");
JSONObject jb = (JSONObject) jsonMainNode.get(0);
JSONArray instructions = jb.getJSONArray("instructions");
JSONObject jb1 = (JSONObject) instructions.get(0);
String id = jb1.getString("id");

关于android - 如何解析 JSON 对象以获取数组中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22796000/

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