I would suggest that you just make a simple ArrayList with your objects, and then serialize them into JSON with a serializer (Using the Jackson library in the example below). It would look something like this:
First, define your model in a class (Made without incapsulations for readability):
public class Country{
public String name;
public Integer population;
public List<String> states;
}
Then you can go ahead and create it, and populate the list:
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonWriter {
public static void main(String[] args) {
Country countryObj = new Country();
countryObj.name = "India";
countryObj.population = 1000000;
List<String> listOfStates = new ArrayList<String>();
listOfStates.add("Madhya Pradesh");
listOfStates.add("Maharastra");
listOfStates.add("Rajasthan");
countryObj.states = listOfStates ;
ObjectMapper mapper = new ObjectMapper();
try {
// Writing to a file
mapper.writeValue(new File("c:\\country.json"), countryObj );
} catch (IOException e) {
e.printStackTrace();
}
}
}
Answer from MichaelCleverly on Stack OverflowI would suggest that you just make a simple ArrayList with your objects, and then serialize them into JSON with a serializer (Using the Jackson library in the example below). It would look something like this:
First, define your model in a class (Made without incapsulations for readability):
public class Country{
public String name;
public Integer population;
public List<String> states;
}
Then you can go ahead and create it, and populate the list:
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonWriter {
public static void main(String[] args) {
Country countryObj = new Country();
countryObj.name = "India";
countryObj.population = 1000000;
List<String> listOfStates = new ArrayList<String>();
listOfStates.add("Madhya Pradesh");
listOfStates.add("Maharastra");
listOfStates.add("Rajasthan");
countryObj.states = listOfStates ;
ObjectMapper mapper = new ObjectMapper();
try {
// Writing to a file
mapper.writeValue(new File("c:\\country.json"), countryObj );
} catch (IOException e) {
e.printStackTrace();
}
}
}
Another way to write a json file in java is to use Filelize.
Filelizer filelizer = new Filelizer("src/main/resources/");
filelizer.save("India", country);
Videos
Google gson is very simple to use both for encoding and decoding.
The simplest way is to fill an object by simply letting the engine fill the fields using reflection to map them to the content of the file as described here : the deserialization is just the call to gson.fromJson(json, MyClass.class); once you created your class.
Seems like you're trying to do what they call Collections in Java. First I would look at your json model. Build a class that holds the properties you listed above. Then the code will look like this.
public void parseJson(){
// Read your data into memory via String builder or however you choose.
List<modelthatyoubuilt> myList = new ArrayList<modelthatyoubuilt>();
JSONArray myAwarry = new JSONArray(dataString);
for(int i = 0; i < myAwarry.size(); i++){
JSONObject j = myAwarry.get(i);
modelthatyoubuilt temp = new modelthatyoubuilt();
temp.setProperty(j.getString("propertyname");
//do that for the rest of the properties
myList.add(temp);
}
public int countObjects(ArrayList<modelthatyoubuilt> s){
return s.size();
}
Hope this helps.