Java JSONObject.toString()正在创建OutOfMemoryError
我必须将StackExchange的数据集转换为Json文件,因此我尝试了以下方法-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
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"
}