2015-04-07 105 views
1

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

代码示例:

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

回答

3

直接写JSON来使用文件:

store.toJson(restaurantMap, file); 

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

我想知道你的restaurantMap可以有多大,用完内存。你真的必须有很多的对象。即使几兆字节String也不应触发OutOfMemoryError

+0

感谢您的回复。我解析[this](https://nycopendata.socrata.com/Health/DOHMH-New-York-City-Restaurant-Inspection-Results/xx67-kt59)JSON约有500000个条目。我也不确定为什么我的内存不足。但是你的方法奏效了。 – Viraj

相关问题