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?