[英]JSONObject.toString() is creating OutOfMemoryError
I have to convert StackExchange's dataset to Json file, so I have tried this-我必须将 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();
}
}
}
But it was having error at jsonObj.toString()
.但它在
jsonObj.toString()
处出错。 A sample xml is -示例 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>
I have tried to use Gson but failed to convert GSONObject
to GsonObject
as GsonParser
need to have GSONObject
's toString()
method which is creating the OutOfMemoryError
.我尝试使用 Gson 但未能将
GSONObject
转换为GsonObject
,因为GsonParser
需要具有GSONObject
的toString()
方法,该方法正在创建OutOfMemoryError
。 Can anyone help in this matter?任何人都可以帮助解决这个问题吗?
There is an underscore-java library with static method U.xmlToJson(xml)
.有一个带有静态方法
U.xmlToJson(xml)
的underscore-java库。 I am the maintainer of the project.我是项目的维护者。
<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>
Output:输出:
{
"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.