使用Java编写JSON文件

14

我想在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

我有这段代码:

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();  
        }  

    }  
}  

请描述预期输出和实际输出。 - dotvav
2
如果我没记错的话,这个 JSON 库相当古老,不支持泛型,这就是为什么会出现这个警告。你可以忽略它。 - Florian Schaetz
2
你的示例运行得很好,类型安全警告请参考https://dev59.com/zGQn5IYBdhLWcg3w_LNH#16415553。 - Omar Mainegra
1个回答

29

我建议您只需使用对象创建一个简单的ArrayList,然后使用序列化器将其转换为JSON(在下面的示例中使用Jackson库)。它会像这样:

首先,在一个类中定义您的模型(为了可读性而不使用封装):

public class Country{
  public String name;
  public Integer population;
  public List<String> states;
}

然后你可以继续创建并填充列表:
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();  
    }  

  }  
}

我会尝试这个。谢谢。 - RuuddR
你有Jackson库的下载链接吗?我唯一找到的是这个,但我没有看到下载链接:https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.7 - RuuddR
最好将您的项目转换为Maven项目,然后在pom.xml中定义库作为依赖项。但是,您也可以使用jackson核心库获得上述功能:https://github.com/FasterXML/jackson-core - MichaelCleverly
1
我下载了这个jar包:http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.5.0/jackson-core-2.5.0.jar,并将其添加到库中,但是在JSONObject和JSONArray这些单词下面出现了红线。 - RuuddR
那是因为它们不是我提供的解决方案的一部分。 - MichaelCleverly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接