-1

How to parsing the JSON in JAVA . Share the JSON formate below.

{ "contacts": [
    {
    "id": "c200",
    "name": "Ravi Tamada",
    "email": "ravi@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender" : "male",
    "phone": {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000",
        "contacts":[ {
          "id": "c2011",
          "name": "Rakesh",
          "email":"rakesh@gmail.com",
          "address":"frtre,sedw,dsfdr",
          "gender" :"female",
          "phone": {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
          } 
        } ]
    }       
    },
    {
    "id": "c201",
    "name": "Johnny Depp",
    "email": "johnny_depp@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender" : "male",
    "phone": {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
     }
    }
    ...
  ]
}

I try to solve this problem since last two day's but it's cant be parsing in java . Also follow the some tutorial but same problem.

So please help me out.

Thanks in advance !

Please help me .

Thank you!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
rahul tripathi
  • 321
  • 2
  • 7
  • 18
  • possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Fabio Antunes Feb 10 '15 at 11:24

4 Answers4

4

Use GSON GSON is a wonderful library from google. JsonParser example:

JsonParser parser = new JsonParser();
JsonObject o = (JsonObject)parser.parse("{\"a\": \"A\"}");

refer this JSON parsing using Gson for Java

Community
  • 1
  • 1
Vishnudev K
  • 2,874
  • 3
  • 27
  • 42
0
JSONObject reader = new JSONObject(downloadResult);//is the result you get by http
JSONObject contacts  = reader.getJSONObject("contacts");
SeanChense
  • 846
  • 8
  • 14
0
try {


            JSONObject jsonObject=JSONObject(jsonresponse);
            JSONArray jsonArray=jsonObject.getJSONArray("contacts");

            if(jsonArray!=null&&jsonArray.length()>0)
            {
                for (int i = 0; i < jsonArray.length(); i++) {

                    jsonObject=jsonArray.getJSONObject(i);

                    String id=jsonObject.getString("id");
                    String name=jsonObject.getString("name");
                    String email=jsonObject.getString("email");
                    String address=jsonObject.getString("address");
                    String gender=jsonObject.getString("gender");

                    JSONObject phone=jsonObject.getJSONObject("phone");

                    String mobile=phone.getString("mobile");
                    String home=phone.getString("home");
                    String office=phone.getString("office");


                    JSONArray contacts=phone.optJSONArray("contacts");

                    if(contacts!=null&&contacts.length()>0)
                    {

                        for (int j = 0; j < contacts.length(); j++) {

                            JSONObject jsonObject2=contacts.getJSONObject(j);

                            id=jsonObject2.getString("id");
                            name=jsonObject2.getString("name");
                            email=jsonObject2.getString("email");
                            address=jsonObject2.getString("address");
                            gender=jsonObject2.getString("gender");

                            jsonObject2=jsonObject.getJSONObject("phone");

                            mobile=phone.getString("mobile");
                            home=phone.getString("home");
                            office=phone.getString("office");

                        }


                    }

                }


            }

        } catch (Exception e) {
            // TODO: handle exception
        }
droidd
  • 1,361
  • 10
  • 15
0

I also recomend GSON, been using it since i can remember.

Example of use:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
DataResult data = gson.fromJson(json,DataResult.class);

The DataResult class should have the same structure as the format of the JSON object you receive. The variable names must be the same...

Hope it helps.

zudo1337
  • 21
  • 3