2017-02-20 81 views
-1

我有解析器json数组和解析器函数,我在列表适配器中使用它,然后适配器由recyclerview使用。它给我的实际长度,但只有而其他人返回NULL我的json解析器为jsonarry只是得到第一个元素

这是我的代码

public void parsee_item() { 
    JSONObject jsonObject = null; 
    try { 
     jsonObject = new JSONObject(json); 
     final JSONArray userss = jsonObject.getJSONArray(JSON_ARRAY); 
     item_id = new String[userss.length()]; 
     item_owner = new String[userss.length()]; 
     item_images = new String[userss.length()]; 
     item_names = new String[userss.length()]; 
     item_price = new String[userss.length()]; 
     item_place = new String[userss.length()]; 


     JSONObject jo = null; 
     for (int i = 0; i < userss.length();i++) { 
      jo = userss.getJSONObject(i); 
      Log.d("alrsp", jo.toString()); 
      item_id[i] = jo.getString(KEY_ID); 
      item_owner[i] = jo.getString(KEY_OWNER); 
      item_images[i] = jo.getString(KEY_IMAGE); 
      item_names[i] = jo.getString(KEY_NAME); 
      item_price[i] = jo.getString(KEY_PRICE); 


     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

数组的第一个元素充满了JSON是

{ 
    "result": [{ 
     "item_id": "10", 
     "owner": "user", 
     "item_type_id": "1", 
     "url1": "http:\/\/localhost:8080\/market\/items\/14.1.png", 
     "name": "jc", 
     "price": "76" 
    }, { 
     "item_id": "12", 
     "owner": "user", 
     "item_type_id": "1", 
     "url1": " http:\/\/localhost:8080\/market\/items\/14.1.png", 
     "name": "nzbsbsb", 
     "price": "0" 
    }, { 
     "item_id": "13", 
     "owner": "user", 
     "item_type_id": "1", 
     "url1": " http:\/\/localhost:8080\/market\/items\/14.1.png", 
     "name": "uygf", 
     "price": "0" 
    }] 
} 

和这张截图的名单 enter image description here

+0

你试图改变你的循环为 “for(int i = 0;我 lal

+0

更改'jo = userss.getJSONObject(4);'to'jo = userss.getJSONObject(i);' –

+0

对不起,我刚刚这是测试的目的,但这是实际代码 –

回答

0

替换该行

jo = userss.getJSONObject(4); 

jo = userss.getJSONObject(i); 
+0

对不起,我只是做了这个测试目的,但这是实际的代码 –

+0

可以分享你的JSON? –

0
 JSONObject jo = null; 
    for (int i = 0; i < userss.length();i++) { 
     jo = userss.getJSONObject(i); 
     Log.d("alrsp", jo.toString()); 
     item_id[i] = jo.getString(KEY_ID); 
     item_owner[i] = jo.getString(KEY_OWNER); 
     item_images[i] = jo.getString(KEY_IMAGE); 
     item_names[i] = jo.getString(KEY_NAME); 
     item_price[i] = jo.getString(KEY_PRICE); 
     item_place[i] = jo.getString("place"); 


    } 
+0

对不起,我只是做了这个测试目的,但这是实际的代码 –

+0

张贴你的json ... – user2025187

2

它不建议解析JSON的自己,用一个JSON解析的Util如GsonfastJson相反。

+1

@OP除非你特别需要,否则不要自己滚动任何东西,整个社区往往比任何个人都更聪明。 – Machinarius

0

我被接收,这不是由jsonarray

item_place[i] = jo.getString("place") 
0

与我们的代码的问题是使用硬编码值4

 jo = userss.getJSONObject(4); 

insted的这种发送的参数使用

jo = userss.getJSONObject(i); 

每一个时间,如果你使用这种类型的分析它可能会导致空指针异常你解析指数4 JSONObject的信息

public void parsee_item() { 
    JSONObject jsonObject = null; 
    try { 
     jsonObject = new JSONObject(json); 
     final JSONArray userss = jsonObject.getJSONArray(JSON_ARRAY); 
     item_id = new String[userss.length()]; 
     item_owner = new String[userss.length()]; 
     item_images = new String[userss.length()]; 
     item_names = new String[userss.length()]; 
     item_price = new String[userss.length()]; 
     item_place = new String[userss.length()]; 


    JSONObject jo = null; 
    for (int i = 0; i < userss.length();) { 
     jo = userss.getJSONObject(i); 
     Log.d("alrsp", jo.toString()); 
     item_id[i] = jo.getString(KEY_ID); 
     item_owner[i] = jo.getString(KEY_OWNER); 
     item_images[i] = jo.getString(KEY_IMAGE); 
     item_names[i] = jo.getString(KEY_NAME); 
     item_price[i] = jo.getString(KEY_PRICE); 
     item_place[i] = jo.getString("place"); 
     i++; 

    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

}

0

您可以分析上述JSON作为如下

private void jsonParse(String response) throws JSONException { 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray jsonArray = jsonObject.getJSONArray("result"); 
     if (jsonArray != null && jsonArray.length() > 0) { 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject object = jsonArray.getJSONObject(i); 
       if (object != null) { 
        String itemId = object.getString("item_id"); 
        String owner = object.getString("owner"); 
        String item_type_id = object.getString("item_type_id"); 
        String url1 = object.getString("url1"); 
        String name = object.getString("name"); 
        String price = object.getString("price"); 
       } 
      } 
     } 
    } 
1

enter image description here

我用你的代码,调试,并发现值更新他们是没有JSON配对错误,因此检查您的回收站视图适配器代码或分享它