问题描述
我试图从数组内的JSONArray
中获取值,因此我能够将整个JSON值成功检索到JSONArray
中,但无法在JSONArray
中获取值.当我将JSONArray
转换为JSONObject
以获取存储在JSONArray
内部的值时.它给出了错误:org.json.JSONException: No value for "banner"
i m trying to get values from JSONArray
inside array, i m able to retrieve whole JSON values into JSONArray
successfully but not able to retrieve values inside JSONArray
. When i convert JSONArray
to JSONObject
to get values stored inside JSONArray
. It gives error: org.json.JSONException: No value for "banner"
这是JSON代码,我使用 jsonlint.com 验证了JSON代码,并显示JSON为Validate,
Here is JSON code, i verified JSON code with jsonlint.com and it showed JSON is Validate,
[
{"code":"banner","moduletitle":0,
"banner":
[
{"image":"http://imageurl"},
{"image":"http://imageurl"},
{"image":"http://imageurl"}
]
}
]
我想从3小时内拿到这个,但没有运气.我是JSON的新手,不知道JSON的实际工作原理,还阅读了 GSON库以获得JSON值.这是我的Java代码.
I m trying to get this from 3 hour but no luck. i m new in JSON and do not know how JSON Actually work, and also read abut GSON Library to get JSON values. here is My Java code.
JSONArray jsonObj = null;
String image_url = "";
String banner_code ="";
try {
jsonObj =new JSONArray(lib_function.getJSONUrl( jsontags.Top_Banner_JOSN_URLs));
Log.d("value retrun :","" +jsonObj);
//---vlaue is coming and print in Log ----//
} catch (JSONException e) {
Log.v("Error in Parser :", " " + e);
Log.d("no value retrun :", "failed to convert");
}
try{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName", jsonObj);
JSONArray subArray = jo.getJSONArray("banner");
image_url= subArray.getString(Integer.parseInt("image"));
Log.d("banner code",""+subArray);
}catch(Exception e)
{
Log.d("not working",""+e);
}
我遵循这个问题,但是很幸运:如何在JSON数组中解析JSON数组Android
I folllow this question but luck:How to parse JSON Array inside another JSON Array in Android
如果有人建议,我会做错事情,将不胜感激.或让我知道,在哪里可以获得有关json的更多信息
If anyone suggest, what i m doing wrong will be appreciate. or let me know, where i can get more information about json
更新非常感谢大家给他们宝贵的时间来回答我的愚蠢问题.所有答案都是正确的,但我只能接受一个答案.非常感谢所有人
UPDATE thanks too all to give their precious time for answering my stupid question. All answers are correct , but i can accept only one answer. A Big thanks to all
推荐答案
此处:
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName", jsonObj);
因为解析jsonObj
JSONArray
,所以无需创建新的JSONArray
和JSONObject
从jsonObj
中提取它.删除上面三行.
Because parsing jsonObj
JSONArray
so no need to create new JSONArray
and JSONObject
to extract it from jsonObj
. remove all above three lines.
banner
JSONArray
在JSONObject
内,由jsonObj
JSONArray
包含,请获取为:
banner
JSONArray
is inside JSONObject
which is contained by jsonObj
JSONArray
, get it as:
JSONObject jsonObject=jsonObj.optJSONObject(0);
JSONArray subArray = jsonObject.getJSONArray("banner");
// get code key from `jsonObject`
String strCode=jsonObject.optString("code");
// get all images urls from `subArray`
for(int index=0;index<subArray.length();index++){
JSONObject imgJSONObject=subArray.optJSONObject(index);
// get image urls
String strImgURL=imgJSONObject.optString("image");
}
此外,如果jsonObj
JSONArray包含多个JSONObject,则使用for-loop
对其进行迭代.
Also, if jsonObj
JSONArray contains multiple JSONObject's then use for-loop
to iterate it.
这篇关于数组内的Json数组检索Android值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!