我希望在每个数组的索引的帮助下获得下面的三个JSONArrays 我的JSON:
{
[
{
"id": 3429,
"name": "Mass Effect: Andromeda",
"prix": 100,
"description": "The newest Mass Effect instalement",
"quantiteDisponible": 400,
"image": "https://res.cloudinary.com/igdb/image/upload/t_screenshot_big/xtn9vhiek0kdpetibmvw.webp"
},
{
"id": 3430,
"name": "Halo 5: Guardians",
"prix": 10,
"description": "Not the best Halo",
"quantiteDisponible": 420,
"image": "https://res.cloudinary.com/igdb/image/upload/t_cover_big/rzjnrhuv5rozj52g9aq3.webp"
}
],
[
{
"id": 3431,
"name": "Bloodbonrne",
"prix": 20,
"description": "Better than you",
"quantiteDisponible": 612,
"image": "https://res.cloudinary.com/igdb/image/upload/t_cover_big/fivw1ogfjw266t72kcn2.webp"
},
{
"id": 3432,
"name": "Dark Souls 3",
"prix": 60,
"description": "Prepare to die",
"quantiteDisponible": 604,
"image": "https://res.cloudinary.com/igdb/image/upload/t_cover_big/ofu6ewg0tzdt5vmzcj9q.webp"
}
],
[
{
"id": 3433,
"name": "Mario Sunshine",
"prix": 40,
"description": "Mario Sunshine best Mario Game",
"quantiteDisponible": 11,
"image": "https://res.cloudinary.com/igdb/image/upload/t_cover_big/ok5aq7j375uaxp59zr2g.webp"
},
{
"id": 3434,
"name": "Outlaw Golf",
"prix": 60,
"description": "Outlaw Golf is back with a bang!",
"quantiteDisponible": 10,
"image": "https://res.cloudinary.com/igdb/image/upload/t_cover_big/cxgtamh2r6z7tezfs1og.webp"
}
]
}
我不知道是否有可能并且非常感谢任何帮助 首先,我将JSON存储为变量:
JSONObject object = new JSONObject(JsonText);
System.out.println(object.length());
System.out.println返回3,因此它知道我们正在讨论3个JSON数组。
这就是我如何尝试获取数组
for (int i = 0; i < object.length(); i++){
JSONArray array = object.getJSONArray(HELP);
}
答案 0 :(得分:1)
你可以尝试这个片段
for (int i = 0; i < getArray.length(); i++) {
JSONObject objects = getArray.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
System.out.println("Key : " + k + ", value : "
+ objects.getString(k));
}
// System.out.println(objects.toString());
System.out.println("-----------");
}
答案 1 :(得分:0)
你必须传递i的值。但是object.getJSONArray()只接受string.So你可以像这样使用。
JSONObject obj=new JSONObject(JsonText);
for(int i=0;i<obj.length();i++){
try {
JSONArray jsonArray=obj.getJSONArray(String.valueOf(i));
} catch (JSONException e) {
e.printStackTrace();
}
}