Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将大列表转换为json字符串_Java_Json_List_Bigdata - Fatal编程技术网

Java 将大列表转换为json字符串

Java 将大列表转换为json字符串,java,json,list,bigdata,Java,Json,List,Bigdata,我有一个包含9864284个元素的列表,我想将该列表转换为json字符串,我使用了递归方法,但我始终有outOfMemory异常 public String createRecuJson(List<CustomObject> inputList, int nElement, int index, List<String> result, String resJson) throws Exception { ObjectMapper objectMapper = n

我有一个包含9864284个元素的列表,我想将该列表转换为json字符串,我使用了递归方法,但我始终有outOfMemory异常

public String createRecuJson(List<CustomObject> inputList, int nElement, int index, List<String> result, String resJson) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    if( index + nElement > inputList.size() ) {
        resJson = resJson.concat(","+objectMapper.writeValueAsString(inputList.subList(index,inputList.size())).replace("[", "").replace("]", ""));
        result.add(objectMapper.writeValueAsString(inputList.subList(index,inputList.size())));
        return resJson;
    }else {
        final List<CustomObject> subListCustomObjects = inputList.subList(index, index+nElement);
        if(resJson.length() == 0)
            resJson.concat(objectMapper.writeValueAsString(subListCustomObjects).replace("[", "").replace("]", ""));
        else
            resJson = resJson.concat(","+objectMapper.writeValueAsString(subListCustomObjects).replace("[", "").replace("]", ""));
        result.add(objectMapper.writeValueAsString(subListCustomObjects));
        return createRecuJson(inputList, nElement, index+nElement, result, resJson);
    }
}
public String createRecuJson(List-inputList、int-neelement、int-index、List-result、String-resJson)抛出异常{
ObjectMapper ObjectMapper=新的ObjectMapper();
if(index+neelement>inputList.size()){
resJson=resJson.concat(“,”+objectMapper.writeValueAsString(inputList.subList(index,inputList.size())).replace(“[”,”).replace(“]”,”);
add(objectMapper.writeValueAsString(inputList.subList(index,inputList.size()));
返回resJson;
}否则{
最终列表subListCustomObjects=inputList.subList(索引,索引+元素);
if(resJson.length()==0)
resJson.concat(objectMapper.writeValueAsString(subListCustomObjects).replace(“[”,”).replace(“[”,”);
其他的
resJson=resJson.concat(“,”+objectMapper.writeValueAsString(subListCustomObjects)。替换(“[”,”)。替换(“],”);
add(objectMapper.writeValueAsString(subListCustomObjects));
返回createRecuJson(inputList,neElement,index+neElement,result,resJson);
}
}

尝试使用
Jackson流媒体API
而不是您的实现。它是对JSON内容的最低写访问级别


您可以找到一些示例。

我使用Apache Spark处理如此大的数据集。即使我只是在笔记本电脑上运行。Spark支持JSON和JSON行。@steven35您可以分享一个示例(例如url)吗?我从不使用apache sparkYou可以尝试使用StringBuilder而不是String resJson,然后返回resJson.toString()。同时使用resJson和result有意义吗?您自己创建JSON有什么具体原因吗?通常,当您使用库而不是专用于大数据集时,您会使用JSON-B之类的工具来完成任务()4GB对于这项工作来说似乎有点有限。也许,这还不足以将整个JSON保存在内存中。。。