I have this JSON string:
[{"user_id":"123","email":"person@email.com","lat":"40.748329","lng":"-73.996223",
"first_name":"Alex","last_name":"Genadinik"}]
and this Java Code:
try
{
JSONObject obj = new JSONObject(result);
Log.d( "NAME: " , "test: " + obj.getString("first_name") );
}
catch ( Exception e )
{
Log.d( "JSON ERRORZ: " , "some crap happened " + e.getMessage() );
}
But it throws this error:
[{"last_name":"Genadinik","first_name":"Alex","lng":"-73.996223","user_id":"1","email":"alex.genadinik@gmail.com","lat":"40.748329"}] of type org.json.JSONArray cannot be converted to JSONObject
Any idea how I can fix this and simply just extract the values in the JSON?
Thanks!
This JSON string is an array, not object, not the square brackets at the beginning (look here). Use
JSONArrayinstead, and extract theJSONObjects from it.