简体   繁体   中英

Gson error: “Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path”

When trying to parse the json text file into an array list of Restaurant objects, I got the error "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path." I am confused where I got it wrong, as the data members in my Restaurant class correspond to the fields in the json file perfectly.

main

                List<Restaurant> restaurants = new ArrayList<Restaurant>();

                ......
                fr = new FileReader(filename);
                br = new BufferedReader(fr);
                Gson gson = new Gson();
                TypeToken<List<Restaurant>> token = new TypeToken<List<Restaurant>>() {};
                restaurants = gson.fromJson(br, token.getType());

Restaurant class

public class Restaurant {

    public String name;
    public String address;
    public double latitude;
    public double longitude;
    public List<String> menu = new ArrayList<String>();
    //public double distance;
    
    public Restaurant(String name, String address, double latitude, double longitude, List<String> menu) {
        this.name = name;
        this.address = address;
        this.latitude = latitude;
        this.longitude = longitude;
        this.menu = menu;
        //this.distance = 0;
    }

}

txt file

{
  "data": [
    {
      "name": "Northern Cafe",
      "address": "2904 S Figueroa Street",
      "latitude": 34.025550,
      "longitude": -118.277440,
      "menu": [
          "orange chicken",
          "rice",
          "noodles",
          "dumplings"
        ]
    },
    {
      "name": "The Lab Gastropub",
      "address": "3500 S Figueroa Street",
      "latitude": 34.019940,
      "longitude": -118.280530,
      "menu": [
          "fried chicken",
          "chicken sandwich",
          "spinach dip",
          "cheeseburger",
          "fries",
          "milkshake"
        ]
    },
    {
      "name": "Taco Bell",
      "address": "3629 S Vermont Ave",
      "latitude": 34.022360,
      "longitude": -118.291850,
      "menu": [
          "chicken taco",
          "beef taco",
          "fries",
          "soda",
          "cheese dipping sauce"
        ]
      
    }
  ]
}

I think your problem is that you are missing the higher structure: this is confirmed by the fact that GSON is expecting a BEGIN_ARRAY, but got a BEGIN_OBJECT. The former is the [ token, and the later the { token.

{
  "data": array
}

This means you should have a mapping:

class Data {
  List<Restaurant> data;
}

And read it using:

TypeToken<Data> token = new TypeToken<Data>() {};
data = gson.fromJson(br, token.getType());

Or you should use the appropriate API (which I don't know, I don't use GSON) to get into the data node first, and then read its value as a List<Restaurant> .

Note that new TypeToken<Data>() {} is a construct probably made to capture a generic when using complex object such as List , Map , and that you may probably ignore with simple type:

data = gson.fromJson(br, Data.class);

You are trying to deserialise into an array of Restaurants but your file has an object, with one field data . Copying the list of restaurants to the outer level should solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM