我将Xml数据转换为Json数据,并且我在xml文件或文本文件中的输入是:-
//**xml input:- <data>34123456.00</data>**
String jsonFileName = "src\\main\\resources\\Light.json";
try {
File xmlFile = new File("src\\main\\resources\\output.txt");
InputStream inputStream = new FileInputStream(xmlFile);
StringBuilder builder = new StringBuilder();
int ptr;
while ((ptr = inputStream.read()) != -1) {
builder.append((char) ptr);
}
String xml = builder.toString();
JSONObject jsonObj = XML.toJSONObject(xml);
System.out.print(jsonObj);
FileWriter fileWriter =
new FileWriter(jsonFileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
bufferedWriter.write(jsonObj.toString(PRETTY_FACTOR));
bufferedWriter.close();
} catch (IOException ex) {
System.out.println(
"Error writing to file '"
+ jsonFileName + "'");
} catch (Exception e) {
e.printStackTrace();
} /* json output is :- {"data":3.4123456E7}
I want Output as :- {"data":34123456.00} */
但是我不想要json格式的指数形式,我想要完整的数字,所以我怎么用Java做到这一点?
答案 0 :(得分:0)
JSONObject jsonObj = XML.toJSONObject(xml,true);
在行上方而不是
JSONObject jsonObj = XML.toJSONObject(xml);
这解决了我的问题。