JSON Array iteration in Android/Java

后端 未结 8 828
广开言路
广开言路 2020-11-22 13:22

I am building an android app that needs to download and synchronise with an online database, I am sending my query from the app to a php page which returns the relevant rows

相关标签:
8条回答
  • 2020-11-22 13:30

    I think this code is short and clear:

    int id;
    String name;
    JSONArray array = new JSONArray(string_of_json_array);
    for (int i = 0; i < array.length(); i++) {
        JSONObject row = array.getJSONObject(i);
        id = row.getInt("id");
        name = row.getString("name");
    }
    

    Is that what you were looking for?

    0 讨论(0)
  • 2020-11-22 13:30

    While iterating over a JSON array (org.json.JSONArray, built into Android), watch out for null objects; for example, you may get "null" instead of a null string.

    A check may look like:

    s[i] = array.isNull(i) ? null : array.getString(i);
    
    0 讨论(0)
  • 2020-11-22 13:33

    Unfortunately , JSONArray doesn't support foreach statements, like:

    for(JSONObject someObj : someJsonArray) {
        // do something about someObj
        ....
        ....
    }

    0 讨论(0)
  • 2020-11-22 13:37

    I have done it two different ways,

    1.) make a Map

            HashMap<String, String> applicationSettings = new HashMap<String,String>();
            for(int i=0; i<settings.length(); i++){
                String value = settings.getJSONObject(i).getString("value");
                String name = settings.getJSONObject(i).getString("name");
                applicationSettings.put(name, value);
            }
    

    2.) make a JSONArray of names

        JSONArray names = json.names();
        JSONArray values = json.toJSONArray(names);
        for(int i=0; i<values.length(); i++){
            if (names.getString(i).equals("description")){
                setDescription(values.getString(i));
            }
            else if (names.getString(i).equals("expiryDate")){
                String dateString = values.getString(i);
                setExpiryDate(stringToDateHelper(dateString)); 
            }
            else if (names.getString(i).equals("id")){
                setId(values.getLong(i));
            }
            else if (names.getString(i).equals("offerCode")){
                setOfferCode(values.getString(i));
            }
            else if (names.getString(i).equals("startDate")){
                String dateString = values.getString(i);
                setStartDate(stringToDateHelper(dateString));
            }
            else if (names.getString(i).equals("title")){
                setTitle(values.getString(i));
            }
        }
    
    0 讨论(0)
  • 2020-11-22 13:40

    You are using the same Cast object for every entry. On each iteration you just changed the same object instead creating a new one.

    This code should fix it:

    JSONArray jCastArr = jObj.getJSONArray("abridged_cast");
    ArrayList<Cast> castList= new ArrayList<Cast>();
    
    for (int i=0; i < jCastArr.length(); i++) {
        Cast person = new Cast();  // create a new object here
        JSONObject jpersonObj = jCastArr.getJSONObject(i);
    
        person.castId = (String) jpersonObj.getString("id");
        person.castFullName = (String) jpersonObj.getString("name");
    
        castList.add(person);
    }
    details.castList = castList;
    
    0 讨论(0)
  • 2020-11-22 13:41

    When I tried @vipw's suggestion, I was faced with this exception: The method getJSONObject(int) is undefined for the type JSONArray

    This worked for me instead:

    int myJsonArraySize = myJsonArray.size();
    
    for (int i = 0; i < myJsonArraySize; i++) {
        JSONObject myJsonObject = (JSONObject) myJsonArray.get(i);
    
        // Do whatever you have to do to myJsonObject...
    }
    
    0 讨论(0)
提交回复
热议问题