0

I'm looking for a good way to loop through JSON object. Looking for example how to do so. Any help is welcome. Need to convert it into Java object. I have read that best way is to use GSON, but could achieve the right result.

{  
   "countries":[  
      {  
         "MobileCountry":{  
            "id":2,
            "name":"England",
            "languages":[  
               {  
                  "MobileLanguage":{  
                     "code":"en",
                     "name":"English"
                  }
               },
               {  
                  "MobileLanguage":{  
                     "code":"ru",
                     "name":"Russian"
                  }
               }
            ],
            "mobile_code":"+60",
            "currency_code":"EUR",
            "regions":[  
               {  
                  "MobileRegion":{  
                     "region_id":2048,
                     "name":"trr"
                  }
               },
               {  
                  "MobileRegion":{  
                     "region_id":1024,
                     "name":"Wonderland"
                  }
               }
            ]
         }
      },
      {  
         "MobileCountry":{  
            "id":1,
            "name":"Spain",
            "languages":[  
               {  
                  "MobileLanguage":{  
                     "code":"esp",
                     "name":"Spanish"
                  }
               },
               {  
                  "MobileLanguage":{  
                     "code":"en",
                     "name":"English"
                  }
               }
            ],
            "mobile_code":"+456",
            "currency_code":"EUR",
            "regions":[  
               {  
                  "MobileRegion":{  
                     "region_id":1,
                     "name":"region_1"
                  }
               },
               {  
                  "MobileRegion":{  
                     "region_id":8,
                     "name":"region_2"
                  }
               },
               {  
                  "MobileRegion":{  
                     "region_id":32,
                     "name":"Region1"
                  }
               },
               {  
                  "MobileRegion":{  
                     "region_id":64,
                     "name":"GaBus"
                  }
               }
            ]
         }
      }
   ]
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Martinu
  • 11
  • 1
  • possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – njzk2 Jan 08 '15 at 18:10

3 Answers3

1

Use JSONObject:

String rawJson = "...."; //escape your string if needed!
JSONObject json = new JSONObject(s); 
bonnyz
  • 13,458
  • 5
  • 46
  • 70
0

Could do more research and post your code to get more useful feedback.

duplicate: How to parse JSON in Android

android has a class for this: http://developer.android.com/reference/android/util/JsonReader.html

Community
  • 1
  • 1
SBC
  • 112
  • 7
0

This is really a broad question, but I'll try to give an overview of a simple implementation of GSON.

In short GSON is:

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Download Gson and add it to your build path. Then, import it to your project:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

Create Gson object to work with:

Gson gson = new GsonBuilder().create();

Then you can create the JSON by simply passing the object into Gson's toJson() method:

gson.toJson(Object);

Or, you could 'consume' a JSON object to create an object back from JSON using the fromJson() method. This works with primitives, or you specify the class the JSON to deserialize into:

Person p = gson.fromJson(reader, Person.class);

Alternatives

You might want to also check out JSONObject. It looks like it is explicitly referenced in the Android documentation. That library is also pretty popular.

James Taylor
  • 6,158
  • 8
  • 48
  • 74