How to parse JSON response in android in the following format
•
Android
I'm trying to parse the JSON response in Android for my Android application. I'm getting org.json.jsonexception. The response is as follows:
{
id: "12345"
email:"abc@gmail.com"
firstName:"abcd"
lastName:"efgh"
userName:"abc123"
}
I'm trying to parse the response as follows:
if (response != null) {
try {
//JSONObject jsonObj = new JSONObject(text);
// Getting JSON Array node
JSONArray contacts = new JSONArray(response.toString());
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
id = c.getString("_id");
email = c.getString("email");
firstName = c.getString("firstName");
lastName = c.getString("lastName");
userName = c.getString("userName");
}
} catch (JSONException e) {
e.printStackTrace();
}
Who can let me know what mistakes I made in parsing the response. All suggestions are welcome
resolvent:
Just replace with "_id" instead of "Id"
id = c.getString("id");
email = c.getString("email");
firstName = c.getString("firstName");
lastName = c.getString("lastName");
userName = c.getString("userName");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码