用java写一个json文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31728446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:31:31  来源:igfitidea点击:

Write a json file in java

javajson

提问by RuuddR

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.

我想用java编写一个json文件,但它不起作用,我收到这个警告:我想知道如何做到这一点,因为我要将一个标签式的cfg文件转换为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();  
        }  

    }  
}  

采纳答案by MichaelCleverly

I would suggest that you just make a simple ArrayList with your objects, and then serialize them into JSON with a serializer (Using the Hymansoin library in the example below). It would look something like this:

我建议您只使用对象创建一个简单的 ArrayList,然后使用序列化程序将它们序列化为 JSON(在下面的示例中使用 Hymansoin 库)。它看起来像这样:

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.Hymanson.JsonGenerationException;
import org.codehaus.Hymanson.map.JsonMappingException;
import org.codehaus.Hymanson.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();  
    }  

  }  
}