gpt4 book ai didi

java - JSONObject.toString() 正在创建 OutOfMemoryError

转载 作者:行者123 更新时间:2023-12-02 02:29:04 24 4
gpt4 key购买 nike

我必须将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。有人可以帮忙解决这个问题吗?

最佳答案

有一个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"
}

关于java - JSONObject.toString() 正在创建 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44990576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com