当JSON语法错误时,如何将JSONObject转换为JSONArray?



我有当前的响应:

"type":"champion",
"format":"standAloneComplex",
"version":"10.11.1",
"data":{
"Aatrox":{
"version":"10.11.1",
"id":"Aatrox",
"key":"266",
"name":"Aatrox",
"title":"the Darkin Blade",
"blurb":"Once honored defenders of Shurima against the Void, Aatrox and his brethren would eventually become an even greater threat to Runeterra, and were defeated only by cunning mortal sorcery. But after centuries of imprisonment, Aatrox was the first to find...",
"info":{
"attack":8,
"defense":4,
"magic":3,
"difficulty":4
},
"image":{
"full":"Aatrox.png",
"sprite":"champion0.png",
"group":"champion",
"x":0,
"y":0,
"w":48,
"h":48
},
"tags":[
"Fighter",
"Tank"
],
"partype":"Blood Well",
"stats":{
"hp":580,
"hpperlevel":90,
"mp":0,
"mpperlevel":0,
"movespeed":345,
"armor":38,
"armorperlevel":3.25,
"spellblock":32.1,
"spellblockperlevel":1.25,
"attackrange":175,
"hpregen":3,
"hpregenperlevel":1,
"mpregen":0,
"mpregenperlevel":0,
"crit":0,
"critperlevel":0,
"attackdamage":60,
"attackdamageperlevel":5,
"attackspeedperlevel":2.5,
"attackspeed":0.651
}
},
"Ahri":{
"version":"10.11.1",
"id":"Ahri",
"key":"103",
"name":"Ahri",
"title":"the Nine-Tailed Fox",
"blurb":"Innately connected to the latent power of Runeterra, Ahri is a vastaya who can reshape magic into orbs of raw energy. She revels in toying with her prey by manipulating their emotions before devouring their life essence. Despite her predatory nature...",
"info":{
"attack":3,
"defense":4,
"magic":8,
"difficulty":5
},
"image":{
"full":"Ahri.png",
"sprite":"champion0.png",
"group":"champion",
"x":48,
"y":0,
"w":48,
"h":48
},
"tags":[
"Mage",
"Assassin"
],
"partype":"Mana",
"stats":{
"hp":526,
"hpperlevel":92,
"mp":418,
"mpperlevel":25,
"movespeed":330,
"armor":20.88,
"armorperlevel":3.5,
"spellblock":30,
"spellblockperlevel":0.5,
"attackrange":550,
"hpregen":6.5,
"hpregenperlevel":0.6,
"mpregen":8,
"mpregenperlevel":0.8,
"crit":0,
"critperlevel":0,
"attackdamage":53.04,
"attackdamageperlevel":3,
"attackspeedperlevel":2,
"attackspeed":0.668
}
}
}
}

(最后两个大括号可能会关闭,因为我不得不缩短响应,因为它太大了( 我正在尝试与每个冠军创建一个 JSONArray,所以 Aatrox 第一,Ahri 第二,等等。

由于此响应的语法,请使用:

String returnString = (source);
JSONObject returnJSON = new JSONObject(returnString);
JSONArray arr= returnJSON.getJSONArray("data");

似乎不起作用,我认为这是因为 JSON 中缺少"[]"(方括号(。有没有办法我仍然可以创建具有此当前状态的 JSONArray?

提前谢谢。

你需要明白JSONObject是键,值对,JSONArray是任何类型对象的列表。你不能只将 jsonObject 转换为 JSONArray。 如果你想转换它你可以做这样的事情..

JSONArray output = new JSONArray();
JSONObject dataObj = returnJSON.getJSONObject("data");
Set<String> keys = dataObj.keySet();
for (String key : keys) {
JSONObject obj = dataObj.getJSONObject(key);
// your logic for populating output
}

使用 https://jsonformatter.curiousconcept.com/分析您的 JSON 文件似乎错过了第一个大括号。添加它们会导致正确的结果。

{
"type":"champion",
"format":"standAloneComplex",
"version":"10.11.1",
"data":{
"Aatrox":{
"version":"10.11.1",
"id":"Aatrox",
"key":"266",
"name":"Aatrox",
"title":"the Darkin Blade",
"blurb":"Once honored defenders of Shurima against the Void, Aatrox and his brethren would eventually become an even greater threat to Runeterra, and were defeated only by cunning mortal sorcery. But after centuries of imprisonment, Aatrox was the first to find...",
"info":{
"attack":8,
"defense":4,
"magic":3,
"difficulty":4
},
"image":{
"full":"Aatrox.png",
"sprite":"champion0.png",
"group":"champion",
"x":0,
"y":0,
"w":48,
"h":48
},
"tags":[
"Fighter",
"Tank"
],
"partype":"Blood Well",
"stats":{
"hp":580,
"hpperlevel":90,
"mp":0,
"mpperlevel":0,
"movespeed":345,
"armor":38,
"armorperlevel":3.25,
"spellblock":32.1,
"spellblockperlevel":1.25,
"attackrange":175,
"hpregen":3,
"hpregenperlevel":1,
"mpregen":0,
"mpregenperlevel":0,
"crit":0,
"critperlevel":0,
"attackdamage":60,
"attackdamageperlevel":5,
"attackspeedperlevel":2.5,
"attackspeed":0.651
}
},
"Ahri":{
"version":"10.11.1",
"id":"Ahri",
"key":"103",
"name":"Ahri",
"title":"the Nine-Tailed Fox",
"blurb":"Innately connected to the latent power of Runeterra, Ahri is a vastaya who can reshape magic into orbs of raw energy. She revels in toying with her prey by manipulating their emotions before devouring their life essence. Despite her predatory nature...",
"info":{
"attack":3,
"defense":4,
"magic":8,
"difficulty":5
},
"image":{
"full":"Ahri.png",
"sprite":"champion0.png",
"group":"champion",
"x":48,
"y":0,
"w":48,
"h":48
},
"tags":[
"Mage",
"Assassin"
],
"partype":"Mana",
"stats":{
"hp":526,
"hpperlevel":92,
"mp":418,
"mpperlevel":25,
"movespeed":330,
"armor":20.88,
"armorperlevel":3.5,
"spellblock":30,
"spellblockperlevel":0.5,
"attackrange":550,
"hpregen":6.5,
"hpregenperlevel":0.6,
"mpregen":8,
"mpregenperlevel":0.8,
"crit":0,
"critperlevel":0,
"attackdamage":53.04,
"attackdamageperlevel":3,
"attackspeedperlevel":2,
"attackspeed":0.668
}
}
}
}

最新更新