Q:

How to create a JSON file in a particular format in Java

I am trying to write a JSON file using Java in a particular format. For example: let's suppose I like to get the JSON file written in the following format: 
{
"resource":[{"name":"Node1"}],

"literals":[{"literal":"A", "B", "C", "D"}]
}

As you may notice, in resource, I would like to write strings in there and in literals, I like to have an arraylist of strings. Here are my Java codes:
public void writeJSON() {

public ArrayList<String> literals  = new ArrayList<String>();  

literals.add("A");
literals.add("B");
literals.add("C");
literals.add("D");

JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();

jsonObject.put("name", resources.getResource());

resources.setLiterals(literals);

jsonObject.put("literal", resources.getLiterals());

try {

    FileWriter file = new FileWriter("/Users/Documents/sample.json");
    file.write(jsonObject.toJSONString());
    file.flush();
    file.close();

        } catch (IOException e) {
          e.printStackTrace();
        }
}

Here is the Java file where I set and get values for the JSON file:
public class Resources {

String resource;
ArrayList<String> literals;
public Resources()
{

}
public void setResource(String resource) 
{  
        this.resource = resource;
}  

public String getResource()
{
    return resource;
}
public void setLiterals(ArrayList<String> literals) 
{  
  this.literals = literals;  
}
public ArrayList<String> getLiterals() 
{  
      return literals;  
}  
}

Could anyone please help me how to create a JSON file based on the format provided above. Your help would be very much appreciated. 

A:

Lets start that Json you are trying to create is invalid.
This is what you have:
{
"resource":[{"name":"Node1"}, 
            {"name":"Node2"}],

"literals":[{"source":"A", "B", "C", "D"},
            {"source":"E", "F", "G", "H"}]
}

As you can see resource looks OK - i.e. it is an object that contains List of objects.
On the other hand literals is an object that contain a List of what? since not {"source":"A", "B", "C", "D"} nor {"source":"E", "F", "G", "H"} is a valid Json.
Figure out this first, then update your java code and we will go from there.
If this: {"resource":[{"name":"Node1"}],"literals":[{"literal":["A","B","C","D"]}]} is desirable, then you can achieve that in the following way(I will use Gson):
JsonWriter jsonWriter = null;
try {
    jsonWriter = new JsonWriter(new FileWriter("test.json"));
    jsonWriter.beginObject();
    jsonWriter.name("resource");
    jsonWriter.beginArray();
    jsonWriter.beginObject();
    jsonWriter.name("name");
    jsonWriter.value("Node1");
    jsonWriter.endObject();
    jsonWriter.endArray();
    jsonWriter.name("literals");
    jsonWriter.beginArray();
    jsonWriter.beginObject();
    jsonWriter.name("literal");
    jsonWriter.beginArray();
    jsonWriter.value("A");
    jsonWriter.value("B");
    jsonWriter.value("C");
    jsonWriter.value("D");
    jsonWriter.endArray();
    jsonWriter.endObject();
    jsonWriter.endArray();
    jsonWriter.endObject();
} catch (IOException e) {
    ...
}finally{
    try {
        jsonWriter.close();
    } catch (IOException e) {
        ...
    }
}

