用Java写一个json文件 [英] Write a json file in java

查看:457
本文介绍了用Java写一个json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java编写一个json文件,但是它不起作用,我收到此警告: 我想知道如何执行此操作,因为我将要转换为tab格式的cfg文件.

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

我有这个代码:

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

    }  
}  

推荐答案

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

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

  }  
}

这篇关于用Java写一个json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆