2017-02-20 27 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配對錯誤,因此檢查您的回收站視圖適配器代碼或分享它