JSONObject.toString()导致OutOfMemoryError错误。

3

我需要将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需要GSONObjecttoString()方法,而该方法导致了OutOfMemoryError。有人能帮忙解决这个问题吗?


1
你明白你正在将整个数据集加载到内存中,因此出现了 OutOfMemoryError 吗?除了为您编写代码外,我不确定任何人能做些什么来帮助您。请查找 StAX 解析器。 - Abhijit Sarkar
如果它在 JSONObject.toString 上仅失败(XML 和 JSON 对象图已完全加载到内存中等),则应该可以使用流编写器 - 参见 https://dev59.com/p3zaa4cB1Zd3GeqPMCSL。流编写器不会首先调用 to-string,因为那样做会打败流的目的。即使不能直接从 JSONObject 中直接使用 Gson 的实现,也应该可以遍历对象图的部分并通过流编写器单独发出这些部分。当然,从头到尾进行流式处理。 - user2864740
1个回答

0

有一个 underscore-java 库,其中有静态方法 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>

输出:

{
  "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"
}    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接