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?

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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
Everything else you have is perfectly fine. Why do you think it isn't?
because it gives exception unable to parse json , kindly review again edited the question
Can you edit again with the actual exception message please?
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")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.