0

I am very new to this, so I have no idea how to do it and I'm still in the process of learning how to parse JSON files. I am trying to parse this JSON Object using Volley, but I am missing the logic on what to do next. Thank you! Here's my code:

public class MainActivity extends AppCompatActivity {
    private TextView results;
    String JsonUrl = "http://api.androidhive.info/contacts/";
    String data = "";
    RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestQueue = Volley.newRequestQueue(this);

        results = (TextView) findViewById(R.id.jsonData);

        JsonObjectRequest objRequest = new JsonObjectRequest(Request.Method.GET,
                JsonUrl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject contactObj = response.getJSONObject("contacts");

                    for (int i = 0; i < contactObj.length(); i++) {
                        String id = contactObj.getString("id");
                        String name = contactObj.getString("name");

                        data += "";
                        data += "Id " + id + "\n\n";
                        data += "Name " + name + "\n\n";
                    }
                    results.setText(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Volley", "Error");
            }
        });
        requestQueue.add(objRequest);
    }
}
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
jlively
  • 735
  • 2
  • 9
  • 29
  • Whats your problem , its all OK , just parse as you are doing, do same for `email,phone,address` etc – Gaurav May 05 '16 at 12:12
  • Possible duplicate of [how to parse JSONArray in android](http://stackoverflow.com/questions/17136769/how-to-parse-jsonarray-in-android) – Nongthonbam Tonthoi May 05 '16 at 12:12
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – OneCricketeer May 05 '16 at 12:31

4 Answers4

0

If the parsing of JSON is correctly getting the id and name at this point the only thing left to do really is use that data. Do you know if it is grabbing that info or not? If it is grabbing the data then really the next thing to do is create a function in your Activity and call that for example

public void processJSONResults(String id, String name) {
    ...Handle processing of data, update GUI etc...
}

Then from your onResponse you would just call processJSONResults with your data. If the JSON processing isn't working let me know, and give me the error / results.

Ira Juneau
  • 294
  • 1
  • 5
0

try like this..

JSONObject contactObj = new JSONObject(response);
JSONArray contactArray = contactObjj.getJSONArray("contacts")
                for (int i = 0; i < contactArray.length(); i++) {
                    JSONObject jsonObj = contactArray.getJSONObject(i);
                    String id = jsonObj.getString("id");
                    String name = jsonObj.getString("name");

                    //Define your logic here to pass the data on results

                results.setText(data);

It will surely help you!

Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
  • Thank you so much! The only thing I had to change was JSONObject contactObj = new JSONObject(response); in JSONObject contactsObj = new JSONObject(String.valueOf(response)); – jlively May 05 '16 at 12:33
  • @jlively...mine pleasure...dear it (String.valueOf(response)) depends upon the format of json response you got and from here i can't tell your json response format...happy coding – Saurabh Vardani May 05 '16 at 12:38
0

This is JSON object:

{"number1":"value1", "number2":"value2", "number3":"value3" }

Use the keys() iterator to iterate over all the properties, and call get() for each.

example code:

Iterator<String> iter = json.keys();
while (iter.hasNext()) {
    String key = iter.next();
    try {
        Object value = json.get(key);
    } catch (JSONException e) {
        // Something went wrong!
    }
}

hope this will help you.. Thanks

Zaigham Raza
  • 364
  • 2
  • 3
  • 14
0

Contacts is an array. Use the following.

JSONArray contactObjArray = response.getJSONArray("contacts");

Now you can iterate the array.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24