简体   繁体   中英

How can I take the elements in a Json array and put the contents in an arraylist?

First off I just want to say I'm a beginner and any help I can get would be great.

I created a car class in a form application. Each car has three propertes: spaceNum, make, and model.

I'm storing each car into an an arraylist. Before exiting the program I write the contents of the arraylist to a file named save.json.

The contents are as follows:

[{"spaceNum":"2","make":"b","model":"b"},{"spaceNum":"3","make":"c","model":"c"},{"spaceNum":"4","make":"d","model":"d"},{"spaceNum":"1","make":"a","model":"a"}]

As you can see there are four Cars stored in my arraylist.

My question is - How can I get the array from my save.json file, where I can then try to put it back into an arraylist?

我将jackson用于所有json序列化,因此,假设您有Car类,则可以执行类似的操作。

List<Car> cars = new ObjectMapper().readValue(new File("save.json"), TypeFactory.defaultInstance().constructCollectionType(List.class, Car.class);

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