I have the following Json data :-
[{"accountid":"1-RMNT","name":"NASA"},
{"accountid":"1-XQN9","name":"NewAccount"},
{"accountid":"1-Q9VF","name":"Noratel Communication"},
{"accountid":"1-RNLY","name":"Nordstrom"}]
How can I convert this JSON data to XML? 开发者_开发知识库Any suggestions would be appreciated.
I don't have a direct solution, but using gson(JSON to object) and xstream(object to XML) is doable. It may need some extra mapping code.
You can check the class XML.java that it´s be included in the package org.jason.
Do not think difficult, Its a easy way to convert XML to JSON or JSON to XML, Just open http://www.utilities-online.info/xmltojson/ and paste your json codes or xml codes. After paste and press button, then you will get Output. Its very useful for you.
Underscore-java has method U.jsonToXml(json)
.
import com.github.underscore.U;
public class Test {
public static void main(String[] args) throws Exception {
String json = "[{\"accountid\":\"1-RMNT\",\"name\":\"NASA\"},\n"
+ "{\"accountid\":\"1-XQN9\",\"name\":\"NewAccount\"},\n"
+ "{\"accountid\":\"1-Q9VF\",\"name\":\"Noratel Communication\"},\n"
+ "{\"accountid\":\"1-RNLY\",\"name\":\"Nordstrom\"}]";
System.out.println(U.jsonToXml(json));
}
}
// <?xml version="1.0" encoding="UTF-8"?>
// <root>
// <element>
// <accountid>1-RMNT</accountid>
// <name>NASA</name>
// </element>
// <element>
// <accountid>1-XQN9</accountid>
// <name>NewAccount</name>
// </element>
// <element>
// <accountid>1-Q9VF</accountid>
// <name>Noratel Communication</name>
// </element>
// <element>
// <accountid>1-RNLY</accountid>
// <name>Nordstrom</name>
// </element>
// </root>
精彩评论