2016-08-16 2 views
0

Я анализирую json данные и у меня есть 2 массива. Я успешно проанализировал первый массив json «item», теперь я понятия не имею, как я могу разобрать 2 «img».Json multiple array parsing is null

Вот мой код:

{ 
    "item": [ 
    { 
     "description": "بافت عالی با قاب", 
     "price": "11000000000000", 
     "country": "ايران", 
     "address": "کوچه مهران یک پلاک7", 
     "region": "البرز", 
     "city": "کرج", 
     "mobile": null, 
     "cat": "حیوانات و مناظر طبیعی", 
     "img": [ 
     "http://tajerfarsh.com/oc-content/themes/tgsh/images/slider/slider11.jpg", 
     "http://tajerfarsh.com/oc-content/themes/tgsh//images/categorys/109.jpg", 
     "http://tajerfarsh.com/oc-content/themes/tgsh//images/categorys/125.jpg" 
     ] 
    } 
    ] 
} 

и вот андроида код на стороне:

public class ParseJSON1 { 
    public static String[] ids; 
    public static String[] descriptions; 
    public static String[] email; 
    public static String[] country; 
    public static String[] address; 
    public static String[] region; 
    public static String[] city; 
    public static String[] cat; 
    public static String[] image; 
    public static String[] mobiles; 

    public static final String JSON_ARRAY = "item"; 
    public static final String KEY_ID = "id"; 
    public static final String DESCCRIPTION = "description"; 
    public static final String EMAIL = "emails"; 
    public static final String COUNTRY = "country"; 
    public static final String ADDRESS = "address"; 
    public static final String REGION = "region"; 
    public static final String CITY = "city"; 
    public static final String CATEGORY = "cat"; 
    public static final String IMAGE = "img"; 
    public static final String MOBILE = "mobile"; 

    private JSONArray users = null; 

    private String json; 

    public ParseJSON1(String json){ 
     this.json = json; 
    } 

    protected void parseJSON1(){ 
     JSONObject jsonObject=null; 
     try { 
      jsonObject = new JSONObject(json); 
      users = jsonObject.getJSONArray(JSON_ARRAY); 



      ids = new String[users.length()]; 
      descriptions = new String[users.length()]; 
      country = new String[users.length()]; 
      address = new String[users.length()]; 
      region = new String[users.length()]; 
      city = new String[users.length()]; 
      cat = new String[users.length()]; 
      email = new String[users.length()]; 
      mobiles = new String[users.length()]; 

      for(int i=0;i<users.length();i++){ 
       JSONObject jo = users.getJSONObject(i); 
       ids[i] = jo.getString(KEY_ID); 
       descriptions[i] = jo.getString(DESCCRIPTION); 
       email[i] = jo.getString(EMAIL); 
       country[i] = jo.getString(COUNTRY); 
       address[i] = jo.getString(ADDRESS); 
       region[i] = jo.getString(REGION); 
       city[i] = jo.getString(CITY); 
       cat[i] = jo.getString(CATEGORY); 
       // image[i] = jo.getString(IMAGE); 
       mobiles[i] = jo.getString(MOBILE); 

      JSONArray img = jsonObject.getJSONArray("img"); 
       image = new String[img.length()]; 
       for (int j=0;j<img.length();j++){ 
        image[i] = jo.getString(IMAGE); 
       } 
      } 



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

Как решить это?

+0

Ohemgee. 1. Используйте List вместо массивов; 2. используйте библиотеку Gson – Damiano

ответ

2

Анализировать это так:

JSONArray img = jo.getJSONArray("img"); 
image = new String[img.length()]; 
for (int j=0;j<img.length();j++){ 
    image[j] = img.getString(j); 
} 

Кроме того, лучше использовать библиотеку как GSON для синтаксического анализа.

+0

java.lang.NullPointerException: попытка чтения из нулевого массива, опять –

+0

Сэр не переделан –

+0

Я обновил свой ответ. 'JSONArray' должен быть извлечен из объекта' jo'. Попробуйте сейчас. –

0

Попробуйте реализовать код, приведенный ниже, этот список используйте вместо массивов, это менее вероятно, чтобы иметь erros (как индекс Bounds)

try { 
      jsonObject = new JSONObject(json); 
      users = jsonObject.getJSONArray(JSON_ARRAY); 



      List<String> ids = new ArrayList<>(); 
      List<String> descriptions = new ArrayList<>(); 
      List<String> country = new ArrayList<>(); 
      List<String> address = new ArrayList<>(); 
      List<String> region = new ArrayList<>(); 
      List<String> city = new ArrayList<>(); 
      List<String> cat = new ArrayList<>(); 
      List<String> email = new ArrayList<>(); 
      List<String> mobiles = new ArrayList<>(); 
      List<String[]> img = new ArrayList<>(); 

      for(int i=0;i<users.length();i++){ 
       JSONObject jo = users.getJSONObject(i); 
       ids.add(jo.getString(KEY_ID)); 
       // Do te rest of the "add" 

       JSONArray imgs = jo.getJSONArray(IMAGE)     

       image = new String[imgs.length()]; 
       for (int j=0;j<imgs.length();j++){       
        image = jo.getString(j); 
        img .add(image); 
       } 
      } 



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