How to parse nested JSON object using the json library?

后端 未结 2 1135
予麋鹿
予麋鹿 2021-01-16 06:52

i want to parse the json object using json library.

    {
    \"batters\":
        {
            \"batter\":
                [
                    { \"id\":          


        
相关标签:
2条回答
  • 2021-01-16 07:28
    {
    "result": "success",
    "countryCodeList":
    [
      {"countryCode":"00","countryName":"World Wide"},
      {"countryCode":"kr","countryName":"Korea"}
    ] 
    }
    

    Here below I am fetching country details

    JSONObject json = new JSONObject(jsonstring);
    JSONArray nameArray = json.names();
    JSONArray valArray = json.toJSONArray(nameArray);
    
    JSONArray valArray1 = valArray.getJSONArray(1);
    
    valArray1.toString().replace("[", "");
    valArray1.toString().replace("]", "");
    
    int len = valArray1.length();
    
    for (int i = 0; i < valArray1.length(); i++) {
    
     Country country = new Country();
     JSONObject arr = valArray1.getJSONObject(i);
     country.setCountryCode(arr.getString("countryCode"));                        
     country.setCountryName(arr.getString("countryName"));
     arrCountries.add(country);
    }
    
    0 讨论(0)
  • 2021-01-16 07:29

    Using JSON..

    JSONObject object = new JSONObject(yourString);
    JSONObject batters = object.getJSONObject("batters");
    JSONArray batter = batters.getJSONArray("batter");
    for(int i = 0 ; i < batter.length() ; i++) { 
    JSONObject object1 = (JSONObject) batter.get(i);
        String id = object1.getString("id");
    }
    
    0 讨论(0)
提交回复
热议问题