我正在尝试使用以下代码解析Json Array
。
我的URL
是http://166.62.17.208/json_preferencess.aspx
我收到以下错误:
Error parsing data org.json.JSONException: No value for id
我已按以下方式显示result
:
[{"items":[{"id":"11","Item_Id":"123","Item_Name":"Chicken Cream /* and the remaining objects of the array*/
我得到的jArray如下:
[{"questions":[{"q_id":"1","q_question":"How would you rate our Menu /* and the remaining objects of the array*/
即使长度超过一个,我也将jArray的长度设为1。
我已在下面发布了我的代码,请一步一步指导我,因为我对json很新。
String result = null;
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://166.62.17.208/json_preferencess.aspx");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success "+"nameValuePairs");
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.e("log_tag", "result "+result.toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
try
{
JSONArray jArray = new JSONArray(result);
Log.w("Lenghsssssss",""+jArray);
String s="",s1,s2,s3,s4,s5,s6,s7,s8,s9;
Log.w("Lengh",""+jArray.length());
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
s=json_data.getString("id");
Log.e("taskid from server","s");
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
答案 0 :(得分:1)
[ // Json Array
{ // Json Object
"items": [// Josn Array
{
"id": "11",
试试这个
JSONArray jArray = new JSONArray(result);
String s="",s1,s2,s3,s4,s5,s6,s7,s8,s9;
Log.w("Lengh",""+jArray.length());
for(int i=0;i<jArray.length();i++){
JSONArray newarr = jArray.getJSONObject(i).getJSONArray("items");
for(int j= 0; j<newarr.length();j++){
JSONObject json_data = newarr.getJSONObject(i);
s=json_data.getString("id");
System.out.println("Id is: "+ s);
}
}