将Gson转换对象直接存储到文件中?

时间:2015-04-07 06:42:51

标签: java json gson

我需要将Map转换为JSON Object并存储到文件中。 由于toJSon对象的Gson方法将Map转换为字符串,因此内存不足。无论如何我可以将Map存储为JSON格式的文件吗?

代码示例:

/**
 * Created by viraj on 4/6/2015.
 */

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HealthInspection {
    private Map<String,Restaurant> restaurantMap;
    public void jsonParser() throws IOException{
        JSONParser parser = new JSONParser();
        FileWriter file = new FileWriter(System.getProperty("user.dir")+"\\src\\restaurants.json");
        try{
            Object obj =  parser.parse(new FileReader(System.getProperty("user.dir")+"\\src\\rows.json"));
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray data = (JSONArray) jsonObject.get("data");
            for (int size=data.size(),i=0;i<size;++i){
                JSONArray temp = (JSONArray) data.get(i);
                String id = (String) temp.get(8);
                Restaurant value = restaurantMap.get(id);
                String inspectionDate = (String) temp.get(16);
                String code = (String) temp.get(18);
                String violationDescription = (String) temp.get(19);
                String score = (String) temp.get(21);
                String grade = (String) temp.get(22);
                String gradeDate = (String) temp.get(23);
                String inspectionType = (String) temp.get(25);
                ViolationSpec v = new ViolationSpec(inspectionDate,code,violationDescription,score,grade,gradeDate,inspectionType);
                if(value==null){
                    String name = (String) temp.get(9);
                    String boro = (String) temp.get(10);
                    String building = (String) temp.get(11);
                    String street = (String) temp.get(12);
                    String zipcode = (String) temp.get(13);
                    String phone = (String) temp.get(14);
                    String type  = (String) temp.get(15);
                    value = new Restaurant(id,name,boro,building,street,zipcode,phone,type,v);
                }
                else{
                    value.addViolationSpec(v);
                }
                restaurantMap.put(id,value);
            }
            Gson store = new GsonBuilder().disableHtmlEscaping().create();
            String json = store.toJson(restaurantMap);
            file.write(json);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        finally {
            file.close();
        }
    }

    public HealthInspection(){
        this.restaurantMap = new HashMap<String,Restaurant>();
    }

    public static void main(String args[]){
        HealthInspection h = new HealthInspection();
        try {
            h.jsonParser();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

使用以下方法直接将Json写入文件:

 store.toJson(restaurantMap, file);

Gson.toJson接受Appendable作为第二个参数,FileWriter实现该接口。

我想知道你的restaurantMap有多大,内存不足。你真的必须拥有很多物品。即使是几个兆字节的String也不应该触发OutOfMemoryError