Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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/1/visual-studio-2012/2.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 JSONObject.toString()正在创建OutOfMemoryError_Java_Json_Xml_Out Of Memory - Fatal编程技术网

Java JSONObject.toString()正在创建OutOfMemoryError

Java JSONObject.toString()正在创建OutOfMemoryError,java,json,xml,out-of-memory,Java,Json,Xml,Out Of Memory,我必须将StackExchange的数据集转换为Json文件,因此我尝试了以下方法- public class Parser { public static int PRETTY_FACTOR=4; public static void main(String[] args) throws Exception { String fileName = "/home/dipannoy/Desktop/stackexchange/android/posthistor

我必须将StackExchange的数据集转换为Json文件,因此我尝试了以下方法-

public class Parser {
    public static int PRETTY_FACTOR=4;
    public static void main(String[] args) throws Exception {  
        String fileName = "/home/dipannoy/Desktop/stackexchange/android/posthistory.json";
        String path= "/home/dipannoy/Desktop/stackexchange/android/PostHistory.xml";

        try {           

            StringBuilder builder =  new StringBuilder();  


            FileInputStream inputStream = null;
            Scanner sc = null;
            try {
                inputStream = new FileInputStream(path);
                sc = new Scanner(inputStream, "UTF-8");
                while (sc.hasNextLine()) {
                    String line = sc.nextLine();
                    builder.append(line);
                }
                if (sc.ioException() != null) {
                    throw sc.ioException();
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (sc != null) {
                    sc.close();
                }
            }


            String xml  = builder.toString();  
            JSONObject jsonObj = XML.toJSONObject(xml);   


            FileWriter fileWriter = new FileWriter(fileName);
            BufferedWriter bufferedWriter =new BufferedWriter(fileWriter);

            bufferedWriter.write(jsonObj.toString());
            bufferedWriter.flush();            
            bufferedWriter.close();
            fileWriter.close();
        }


          catch(IOException ex) {
                System.out.println(
                    "Error writing to file '"
                    + fileName + "'");

            } catch(Exception e) {  
                e.printStackTrace();  
            }
    }  
}
但它在
jsonObj.toString()
处出错。 示例xml为-

<comments>
<row Id="1" PostId="1" Score="4" Text="Did I just place the first upvote?  Congrats on getting this site off the ground!" CreationDate="2016-01-12T18:47:12.573" UserId="23"/>
</comments>

我尝试使用Gson,但未能将
GSONObject
转换为
GSONObject
,因为
GsonParser
需要
GSONObject
toString()
方法来创建
OutOfMemoryError
。有人能帮上忙吗?

有一个静态方法库
U.xmlToJson(xml)
。我是项目的维护者

<comments>
<row Id="1" PostId="1" Score="4" Text="Did I just place the first upvote?  Congrats on getting this site off the ground!" CreationDate="2016-01-12T18:47:12.573" UserId="23"/>
</comments>

您是否知道您正在将整个数据集加载到内存中,从而导致
OutOfMemoryError
?除了为您编写代码之外,我不确定其他人能做些什么来帮助您。查找StAX解析器。如果它在JSONObject.toString上失败(XML和JSON对象图已经完全加载到内存中,等等),那么应该可以使用streaming writer-ref。streaming writer不会首先调用string,因为这会破坏流的目的。即使(Gson的实现)不能直接从JSONObject使用,也应该可以遍历对象图的各个部分,并通过流式编写器分别发出这些部分。当然,从端到端的流媒体。。
{
  "comments": {
    "row": {
      "-Id": "1",
      "-PostId": "1",
      "-Score": "4",
      "-Text": "Did I just place the first upvote?  Congrats on getting this site off the ground!",
      "-CreationDate": "2016-01-12T18:47:12.573",
      "-UserId": "23",
      "-self-closing": "true"
    }
  },
  "#omit-xml-declaration": "yes"
}