How to convert Graphql response to object in java (Android Studio) || String to object in java

Convert Graphql response or json to object in java 





Tips: We are going to use JSONObject, JSONArray class and getJSONObject and getJSONArray method to convert json to java object or pojo class object. we are going to understand with the help of few example. Let’s go 

Example 1 - Let’s take one graphql response or a json 

{

    "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"

                }

        },

        {

                "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"

                }

        },

        .

        .

        .

        .

  ]

}


Now, lets try to convert json to java object


if (jsonStr != null) {
  try {
    JSONObject jsonObj = new JSONObject(jsonStr);

    // Getting JSON Array node
    JSONArray contacts = jsonObj.getJSONArray("contacts");

    // looping through All Contacts
    for (int i = 0; i < contacts.length(); i++) {
      JSONObject c = contacts.getJSONObject(i);

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

      // Phone node is JSON Object
      JSONObject phone = c.getJSONObject("phone");
      String mobile = phone.getString("mobile");
      String home = phone.getString("home");
      String office = phone.getString("office");

      // tmp hash map for single contact
      HashMap < String, String > contact = new HashMap < > ();

      // adding each child node to HashMap key => value
      contact.put("id", id);
      contact.put("name", name);
      contact.put("email", email);
      contact.put("mobile", mobile);

      // adding contact to contact list
      contactList.add(contact);
    }
  } catch (final JSONException e) {
    Log.e(TAG, "Json parsing error: " + e.getMessage());
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Toast.makeText(getApplicationContext(),
            "Json parsing error: " + e.getMessage(),
            Toast.LENGTH_LONG)
          .show();
      }
    });

  }
} 


Example 2 - Let's take another example, 

{
  "data": {
    "allEvents": {
      "edges": [
        {
          "node": {
            "title": "title 1"
          }
        },
        {
          "node": {
            "title": "title 2"
          }
        },
        {
          "node": {
            "title": "title 3"
          }
        },
        {
          "node": {
            "title": "title 4"
          }
        },
        {
          "node": {
            "title": "title 5"
          }
        }
      ]
    }
  }
}


Now, lets try to convert json to java object

if (jsonStr != null) {
  try {
    JSONArray jsonEvents = new JSONObject(jsonStr).getJSONObject("data").getJSONObject("allEvents").getJSONArray("edges");
    for (int i = 0; i < jsonEvents.length(); ++i) {
      JSONObject event = jsonEvents.getJSONObject(i).getJSONObject("node");
      Log.v("TAG", gson.fromJson(event.toString(), Event.class).toString());
    }
  } catch (JSONException e) {
    e.printStackTrace();
  }
}


Post a Comment

0 Comments