0

I'm quite new to JSON and it's operation I was trying to retrieve JSON from server

 datafromServer = bufferedReader.readLine();
                inputStream.close();
JSONArray jsonArray = new JSONArray(datafromServer );

and came accross to this exception :

org.json.JSONException: Value {"last_name":"Hicks","id":18,"email":"dwaynehicks@usssulaco.com","first_name":"Dwayne"} of type org.json.JSONObject cannot be converted to JSONArray

So I found this answer :

JSONObject object = new JSONObject(result);
JSONArray Jarray = object.getJSONArray("contacts");

for (int i = 0; i < Jarray.length(); i++) {
   JSONObject Jasonobject = Jarray.getJSONObject(i);

Now, the problem is, in the answer, OP had object formation of :

 "contacts": [{
        .....
        .....

         ]

in this JSON. While I get formation of (no key mapped to this line) :

{"last_name":"Hicks","id":18,"email":"dwaynehicks@usssulaco.com","first_name":"Dwayne"}

So how I modified this line *JSONArray Jarray = object.getJSONArray("contacts");*? I don't have such key.

Thanks

Community
  • 1
  • 1

3 Answers3

2

You don't need a JSONArray you can just get the data from the JSONObject like this:

JSONObject jsonObject = new JSONObject(result);
String lastName = jsonObject.getString("last_name");
int id = jsonObject.getInt("id");
String email = jsonObject.getString("email");
String firstName = jsonObject.getString("first_name");
Dennis van Opstal
  • 1,294
  • 1
  • 22
  • 44
2

{"last_name":"Hicks","id":18,"email":"dwaynehicks@usssulaco.com","first_name":"‌​Dwayne"}

^ Is an instance of a single JSON object.

{"contacts": [{...}, {...}]}

^ Is an instance of a single JSON Array. A JSON array can have 0 to many JSON Object in it.

The first error is caused by trying to make a JSONArray with a String that represents a JsonObject.

The "answer" that you found is an example of someone reading a JsonObject, getting a JsonArray (contacts) from that object, and then looping through all JsonObjects in that JsonArray. This doesn't apply to you because you simply have one flat JsonObject.

Read up on the API to find which methods are appropriate for getting each type. In this case, since you have a String object that is exactly one JsonObject you can use JsonObjects constructor that takes a String like @SripadRaj said in the comments.

ezPaint
  • 152
  • 1
  • 8
  • So you have just fixed my understanding about the structure thank you – Plain_Dude_Sleeping_Alone Jun 14 '16 at 13:53
  • If you ever have problems understanding a JSON, copy and paste it into an online JSON editor. Many of them have tools that will turn the text version of it into a visual representation. http://www.jsoneditoronline.org/ – ezPaint Jun 14 '16 at 14:09
  • Thank you, though, how can possibly a json editor makes me understand the json itself :D – Plain_Dude_Sleeping_Alone Jun 14 '16 at 14:33
  • 1
    @Hey-men-whatsup Take a JSON object that is confusing. Paste it in the left and then hit the right array in the center. On the right you will see a (maybe for some) easier to understand display of the JSON. – ezPaint Jun 14 '16 at 14:57
1

Json code needs to get into the try catch

   try{
    JSONObject object = new JSONObject(result);
    JSONArray Jarray = object.getJSONArray("contacts");

       for (int i = 0; i < Jarray.length(); i++) {
          JSONObject Jasonobject = Jarray.getJSONObject(i);
            String lastName = jsonObject.getString("last_name");
            int id = jsonObject.getInt("id");
            String email = jsonObject.getString("email");
            String firstName = jsonObject.getString("first_name");
       }
    }catch (JSONException e) {
       e.printStackTrace();
    }
Ahmet Kocaman
  • 109
  • 11