Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a json file in java

Tags:

java

json

I want to write a json file in java, but it doesn't work, I get this warning: I want to know how to do this, because I am going to convert a cfg file that is tabbed to json.

Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic type ArrayList<E> should be parameterized

and I have this code:

package json;  

import java.io.File;  
import java.io.FileWriter;  
import java.io.IOException;  

import org.json.simple.JSONArray;  
import org.json.simple.JSONObject;

public class JsonWriter {  

    public static void main(String[] args) {  

        JSONObject countryObj = new JSONObject();  
        countryObj.put("Name", "India");  
        countryObj.put("Population", new Integer(1000000));  

        JSONArray listOfStates = new JSONArray();  
        listOfStates.add("Madhya Pradesh");  
        listOfStates.add("Maharastra");  
        listOfStates.add("Rajasthan");  

        countryObj.put("States", listOfStates);  

        try {  

            // Writing to a file  
            File file=new File("JsonFile.json");  
            file.createNewFile();  
            FileWriter fileWriter = new FileWriter(file);  
            System.out.println("Writing JSON object to file");  
            System.out.println("-----------------------");  
            System.out.print(countryObj);  

            fileWriter.write(countryObj.toJSONString());  
            fileWriter.flush();  
            fileWriter.close();  

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

    }  
}  
like image 341
RuuddR Avatar asked Jul 30 '15 15:07

RuuddR


People also ask

What is JSON file in Java?

JSON (JavaScript Object Notation) is a lightweight data-interchange format, and we most commonly use it for client-server communication. It's both easy to read/write and language-independent. A JSON value can be another JSON object, array, number, string, boolean (true/false) or null.

Can Java use JSON files?

The Java API for JSON Processing provides portable APIs to parse, generate, transform, and query JSON. JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write.


1 Answers

I would suggest that you just make a simple ArrayList with your objects, and then serialize them into JSON with a serializer (Using the Jacksoin 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();  
    }  

  }  
}
like image 160
MichaelCleverly Avatar answered Oct 20 '22 01:10

MichaelCleverly