java 里 json 数组字符串 转 XML

json数组字符串转换成xml

@Test

public void testJsonList()
{
String str = "[{\"t\":\"tt1\", \"t1\":\"haha\"},{\"t\":\"tt2\"},{\"t\":\"tt3\"}]";
System.out.println("json:" + str);
//字符串转换成jsonArray
JSONArray b = JSONArray.fromObject(str);


XMLSerializer xmlWriter = new XMLSerializer();
//设置xml root node name
xmlWriter.setRootName("confList");

String xml = xmlWriter.write( b);
xml = xml.replaceAll(" type=\"string\"", "");
//设置xml node name
xml = xml.replaceAll("<e class=\"object\">", "<confInfo>");
xml = xml.replaceAll("</e>", "</confInfo>");

System.out.println("xml:" + xml);

}


json字符串转换成xml
@Test
public void testJson()
{
String str = "{\"user\":\"\", \"haha\":\"ff\"}";
System.out.println("json:" + str);
JSONObject json = JSONObject.fromObject(str);
XMLSerializer xmlWriter = new XMLSerializer();
//设置root node name
xmlWriter.setRootName("userInfo");
String xml = xmlWriter.write( json );
xml = xml.replaceAll(" type=\"string\"", "");
System.out.println("xml:" + xml);
}

你可能感兴趣的:(java,json,xml,String,object,Class)