1

I have got a JSON REST API response from an URL.
Now I am unable parse the JSON data.
I tried many solutions but could not get the data.
The response is quite messed up, but it contains the required data.
All the JSON objects are in one array.

Here is the JSON response:

[{"umeta_id":"1","user_id":"1","meta_key":"nickname","meta_value":"zfdz"},{"umeta_id":"2","user_id":"1","meta_key":"first_name","meta_value":""},{"umeta_id":"3","user_id":"1","meta_key":"last_name","meta_value":""},{"umeta_id":"4","user_id":"1","meta_key":"description","meta_value":""},{"umeta_id":"5","user_id":"1","meta_key":"rich_editing","meta_value":"true"},{"umeta_id":"6","user_id":"1","meta_key":"comment_shortcuts","meta_value":"false"},{"umeta_id":"7","user_id":"1","meta_key":"admin_color","meta_value":"fresh"},{"umeta_id":"8","user_id":"1","meta_key":"use_ssl","meta_value":"0"},{"umeta_id":"9","user_id":"1","meta_key":"show_admin_bar_front","meta_value":"true"}]

code"

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

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

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

            String name = c.getString("umeta_id");
            String email = c.getString("user_id");
            String mobile = c.getString("meta_value");


            HashMap<String, String> contact = new HashMap<>();

            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();
            }
        });

    }
} else {
    Log.e(TAG, "Couldn't get json from server.");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(),
                    "Couldn't get json from server. Check LogCat for possible errors!",
                    Toast.LENGTH_LONG)
                    .show();
        }
    });

}

The JSON is valid, I checked it by using an online tool.
But I want the data in my application.
What am I doing wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Addi.Star
  • 475
  • 2
  • 15
  • 36

1 Answers1

1

Firstly. jsonObj.getJSONArray("");... The key cannot be an empty string.*

Your entire response is a JSONArray since it starts with a [ character and not a {, so you should not use this

JSONObject jsonObj = new JSONObject(jsonStr); 

but instead you use this, and delete the other line

JSONArray contacts = new JSONArray(jsonStr); 

Then you start your for loop from there.

Related - How to parse JSON in Android


* There may be other issues with the code, but you have not stated what those are

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • code is running fine i can recieve the response in android but it a lot bunch of json string , can you show me a example how to get the feilds i am intrested in – Addi.Star Dec 23 '16 at 00:06
  • Everything else you have is perfectly fine. Why do you think it isn't? – OneCricketeer Dec 23 '16 at 00:08
  • because it gives exception unable to parse json , kindly review again edited the question – Addi.Star Dec 23 '16 at 00:14
  • Can you edit again with the actual exception message please? – OneCricketeer Dec 23 '16 at 00:16
  • Now that you're getting the JSONArray (an array of JSONObjects), you can iterate on each object and use the getter methods on the object for the fields you want, like `jsonObj.getString("meta_value")` or `jsonObj.getLong("user_id")` – bytehala Dec 23 '16 at 03:10
  • @lemuel Who are you responding to? The question already has `c.getString("umeta_id");`, which is correct, as I said – OneCricketeer Dec 23 '16 at 03:23
  • @cricket_007 I'm just responding to his first comment, geez – bytehala Dec 23 '16 at 03:26