1

I'm working on android app that should read this json string:

{
"coord":{
       "lon":145.77,
       "lat":-16.92
},
"weather":[
          {
        "id":803,
        "main":"Clouds",
        "description":"broken clouds",
        "icon":"04n"
        }
        ],
  "base":"cmc stations"
}

Something similar to this.

I can read the "coord" values successfully using the following method:

public Coordinates readCoordinates(JsonReader reader) throws IOException{
    double longitude = 0.0;     // lon
    double latitude  = 0.0;     // lat

    reader.beginObject();

    while (reader.hasNext()){
        String nameToRead = reader.nextName();
        if(nameToRead.equals("lon")){
            longitude = reader.nextDouble();
        }else if (nameToRead.equals("lat")){
            latitude = reader.nextDouble();
        }else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return (new Coordinates(longitude, latitude));
}

I have a similar method for reading the "weather":

public Weather readWeather(JsonReader reader) throws IOException{
    int id = 0;
    String main         = "";
    String description  = "";
    String icon         = "";

    reader.beginObject();
    while (reader.hasNext()){
        String nameToRead = reader.nextName();
        if(nameToRead.equals("id")){
            id = reader.nextInt();
        }else if (nameToRead.equals("main")){
            main = reader.nextString();
        }else if (nameToRead.equals("description")){
            description = reader.nextString();
        }else if (nameToRead.equals("icon")){
            icon = reader.nextString();
        }else{
            reader.skipValue();
        }
    }
    reader.endObject();
    return (new Weather(id, main, description, icon));
}

I keep getting this exception message Expected BEGIN_OBJECT but was BEGIN_ARRAY

If I change reader.beginObject() to reader.beginArray() I get the same error. I also tried removing it entirely, and the same error occurred.

I'm assuming this is caused by the introduction of the [, but I'm not really sure how to fix this. If anyone has a clue please help, I would really appreciate it, thank you.

Sbonelo
  • 664
  • 1
  • 9
  • 25
  • 1
    Instead of JsonReader you can use the easy one `JSONObject jsonobject = new JSONObject(yourJsonString);` .. Then use `String description = jsonObject.getString("description");`.....Example: If you need "coord" JsonObject you can use `JSONObject jsonobject = new JSONObject("coord");` and then use `int longitude = jsonObject.getInt("lon");` – Akshay Bhat 'AB' Dec 04 '15 at 11:42
  • 1
    this code is obvious and looks good ... the whole point that readWeather should be called in loop and loop should be wrapped with array (begin and end) ... just like in android's documentation – Selvin Dec 04 '15 at 11:46

2 Answers2

2

you are not getting the structure of JSON. after "weather" there array which have only one object. so your code should be

 jsonReader.beginArray();

                while( jsonReader.hasNext() ) {

                    jsonReader.beginObject();

                    while( jsonReader.hasNext() ) {

                       String nameToRead = reader.nextName();
    if(nameToRead.equals("id")){
        id = reader.nextInt();
    }else if (nameToRead.equals("main")){
        main = reader.nextString();
    }else if (nameToRead.equals("description")){
        description = reader.nextString();
    }else if (nameToRead.equals("icon")){
        icon = reader.nextString();
                        else {

                            jsonReader.skipValue();

                        }
curiousMind
  • 2,812
  • 1
  • 17
  • 38
0

Inside your gson model class just have a look. As per json

"weather":[
          {
        "id":803,
        "main":"Clouds",
        "description":"broken clouds",
        "icon":"04n"
        }
        ]

it should be parse as jsonArray but I think inside your model class you are expecting it as jsonObject.

Use http://www.jsonschema2pojo.org/ to get exact java model class as per json.

Praveen Sharma
  • 4,326
  • 5
  • 25
  • 45