1

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?

kosa
  • 63,683
  • 12
  • 118
  • 157
  • possible duplicate of http://stackoverflow.com/questions/2255220/how-to-parse-a-json-and-turn-its-values-into-an-array – T I Aug 03 '12 at 14:40
  • You should look into [Jackson](http://jackson.codehaus.org/) or [GSON](http://code.google.com/p/google-gson/) for converting back and forth from JSON to Java objects. – Thomas Aug 03 '12 at 14:42

1 Answers1

0

I use jackson for all my json serialization, so assuming you have a Car class, you can do something like this.

List<Car> cars = new ObjectMapper().readValue(new File("save.json"), TypeFactory.defaultInstance().constructCollectionType(List.class, Car.class);
Ransom Briggs
  • 2,927
  • 3
  • 30
  • 46