簡體   English   中英

JSONObject.toString() 正在創建 OutOfMemoryError

[英]JSONObject.toString() is creating OutOfMemoryError

我必須將 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 任何人都可以幫助解決這個問題嗎?

有一個帶有靜態方法U.xmlToJson(xml)underscore-java庫。 我是項目的維護者。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM