gpt4 book ai didi

Android 用数组中的数组解析 JSON

转载 作者:行者123 更新时间:2023-11-29 21:19:25 25 4
gpt4 key购买 nike

帮我解析JSON。我在没有“时刻”的情况下解析这个 JSON,也不知道如何用“时刻”解析。我的 JSON 响应:

{
"A": [
{
"time0_90": "20",
"score": "0 : 0",
"team1": "Россия",
"team2": "Франция",
"group": "A",
"live": "1"
},
{
"time0_90": "20",
"score": "0 : 0",
"team1": "Португалия",
"team2": "Гондурас",
"group": "A",
"live": "0",
"time": "18:30",
"stadium": "",
"referee": "судья"
}
],
"B": [
{
"time0_90": "3",
"score": "1 : 0",
"moment": [
{
"class_moment": "g",
"name1": "Халк",
"time0_90Moment": "5",
"team": "1"
},
{
"class_moment": "sub",
"name1": "Фред",
"time0_90Moment": "50",
"team": "1",
"name2": "Жо"
}
],
"team1": "Бразилия",
"team2": "Испания",
"group": "B",
"live": "1"
}
],
"C": [],
"D": [],
"E": [],
"F": [
{
"time": "15:00",
"stadium": "Маракана",
"referee": "судья",
"team1": "Россия",
"team2": "Франция",
"group": "F",
"live": "0"
}
],
"G": [],
"H": []
}

此代码是没有“moment”的解析响应。如何解析带有“moment”的响应并将其保存在数组中?

JSONObject jsonResponse = new JSONObject(jsonResult);
for (int j=0; j<8; j++){
String[] groupName = {"A", "B", "C", "D", "E", "F", "G", "H"};
JSONArray jsonMainNode = jsonResponse.optJSONArray(groupName[j]);
if (jsonMainNode.length() == 0){ continue;}
else {
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
team1 = jsonChildNode.optString("team1");
team2 = jsonChildNode.optString("team2");
group = jsonChildNode.optString("group");
int live = jsonChildNode.getInt("live");
String time = null;
if (live == 1){
time = jsonChildNode.optString("time0_90")+"'";
score = jsonChildNode.optString("score");
}
else {
score = jsonChildNode.optString("time");
referee = jsonChildNode.optString("referee");
time = jsonChildNode.optString("stadium");
}

最佳答案

试试这个:

jsonArray moment = json.getJsonArray("B").getJsonObject(0).getJsonArray("moment");
String class = moment.getString("class_moment");
String name= moment.getString("name1");
String time= moment.getString("time0_90Moment");
String team= moment.getString("team");

关于Android 用数组中的数组解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940826/

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