使用GSON加载非常重的流

时间:2013-06-03 17:28:53

标签: java android json gson

我正在尝试读取非常繁重的JSON(超过6000个对象)并将它们存储在哈希映射中以便稍后将其插入到我的数据库中。

但问题是我面对OOM,这是因为我的JSON很重要,但GSON库应该让我摆脱这种情况,但事实并非如此!

有什么想法吗?

public Map<String,String> readJsonStream(InputStream in) throws IOException 
{
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    Map<String,String> contentMap = new HashMap<String,String>();

    Gson mGson = new Gson(); 

    contentMap = mGson.fromJson(reader, contentMap.getClass());

    reader.close();

    return contentMap;
}

1 个答案:

答案 0 :(得分:7)

根据我的经验,是的,你可以使用谷歌GSON流式传输JSON数据,这是一个如何做的例子:

APIModel result = new APIModel();
        try {
            HttpResponse response;
            HttpClient myClient = new DefaultHttpClient();
            HttpPost myConnection = new HttpPost(APIParam.API_001_PRESENT(
                    serial_id, api_key));
            try {
                response = myClient.execute(myConnection);
                Reader streamReader = new InputStreamReader(response
                        .getEntity().getContent());
                JsonReader reader = new JsonReader(streamReader);
                reader.beginObject();
                while (reader.hasNext()) {

                    String name = reader.nextName();

                    if (name.equals("result")) {
                        if (reader.nextString() == "NG") {
                            result.setResult(Util.API_001_RESULT_NG);
                            break;
                        }
                    } else if (name.equals("items")) {
                        result = readItemsArray(reader);
                    } else {
                        reader.skipValue(); // avoid some unhandle events
                    }
                }

                reader.endObject();
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
                result.setResult(Util.API_001_RESULT_NG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setResult(Util.API_001_RESULT_NG);
        }

readItemsArray函数:

// read items array
    private APIModel readItemsArray(JsonReader reader) throws IOException {
        APIModel result = new APIModel();
        String item_name, file_name, data;
        result.setResult(Util.API_001_RESULT_OK);

        reader.beginArray();
        while (reader.hasNext()) {
            item_name = "";
            file_name = "";
            data = "";
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equals("name")) {
                    item_name = reader.nextString();
                } else if (name.equals("file")) {
                    file_name = reader.nextString();
                } else if (name.equals("data")) {
                    data = reader.nextString();
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
            result.populateModel("null", item_name, file_name, data);
        }
        reader.endArray();
        return result;
    }

API模型类:

public class APIModel {
    private int result;
    private String error_title;
    private String error_message;
    private ArrayList<String> type;
    private ArrayList<String> item_name;
    private ArrayList<String> file_name;
    private ArrayList<String> data;

    public APIModel() {
        result = -1;
        error_title = "";
        error_message = "";
        setType(new ArrayList<String>());
        setItem_name(new ArrayList<String>());
        setFile_name(new ArrayList<String>());
        setData(new ArrayList<String>());
    }

    public void populateModel(String type, String item_name, String file_name, String data) {
        this.type.add(type);
        this.item_name.add(item_name);
        this.file_name.add(file_name);
        this.data.add(data);
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    public String getError_title() {
        return error_title;
    }

    public void setError_title(String error_title) {
        this.error_title = error_title;
    }

    public String getError_message() {
        return error_message;
    }

    public void setError_message(String error_message) {
        this.error_message = error_message;
    }

    public ArrayList<String> getType() {
        return type;
    }

    public void setType(ArrayList<String> type) {
        this.type = type;
    }

    public ArrayList<String> getItem_name() {
        return item_name;
    }

    public void setItem_name(ArrayList<String> item_name) {
        this.item_name = item_name;
    }

    public ArrayList<String> getFile_name() {
        return file_name;
    }

    public void setFile_name(ArrayList<String> file_name) {
        this.file_name = file_name;
    }

    public ArrayList<String> getData() {
        return data;
    }

    public void setData(ArrayList<String> data) {
        this.data = data;
    }


}

在我使用谷歌GSON的流媒体API之前我也有OOM错误,因为我得到的JSON数据是非常大的数据(Base64编码中的许多图像和声音)但是使用GSON流我可以克服该错误,因为它读取数据每个令牌不是一次全部。对于Jackson JSON库,我认为它也有流API,以及如何使用它与谷歌GSON的实现几乎相同。我希望我的回答可以帮助你,如果你对我的回答有另一个问题,请随时在评论中提问:)