Many data with brackets [] appeared in the project database. The test reported a bug. After troubleshooting, it was caused by using json-lib jar package. json-lib converts null values to [] during the process of converting xml strings to json format. Now I will briefly summarize the similarities and differences between the two XML to JSON packages. Maybe because such questions are relatively basic, although someone asked such questions online, no answers were found. In order to facilitate junior programmers like me to discover and solve problems as soon as possible, I wrote this blog post.
Most of the jar packages for xml to json that can be found online are net.sf.json-lib. This package is used more in json parsing, but the disadvantage is that it requires more dependency packages. But here I will only talk about his XML to JSON.
1.net.sf.json-lib to json;
JSON json =xmlSerializer.read(xml);
The problem with this method is that it will convert the null value to []. Use json.get(value).getclass to obtain the type of []. You can find that json-lib recognizes the null value as a jsonarray type instead of a string, and the tostring method of jsonarray is rewritten as [,,,,]
If you still want to use this method, you can make the following modifications. When using the getString() method to obtain the json object value converted from xml, first determine whether the type is string and then obtain it.
json.get(value) instanceof String ? json.getString(value):""
2. Org.json to json:
org.json.JSONObject jsonObj = org.json.XML.toJSONObject(xml);
The [] value will not be generated, but the root tag will be retained (the standard XML document has and only one root tag, the so-called root tag is a pair of <root tags></root tags> containing all other tags), and the general root tag is meaningless to the data, so if you need to do the processing
jsonObj.get("root");Here is the test code:
package com.pptv.ppvision.util; import org.json.XML; import net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class Xml2JsonTest { private static String xml = "<root><user><name>weless</name><sex></sex></user></root>"; public static void main(String[] args) { testOrgJSon(); testXmlSerializer(); } public static void testOrgJSon(){ org.json.JSONObject jsonObj = XML.toJSONObject(xml); System.out.println("org.json xml2json:"+jsonObj); org.json.JSONObject user = jsonObj.getJSONObject("root").getJSONObject("user");//org.json need to remove the root tag System.out.println("user name: "+user.getString("name")); System.out.println("Gender:"+user.getString("sex")); } public static void testXmlSerializer(){ XMLSerializer xmlSerializer = new XMLSerializer(); JSONObject jsonObject = (JSONObject)xmlSerializer.read(xml); System.out.println("json-lib xml2json result: "+jsonObject); JSONObject user = jsonObject.getJSONObject("user"); System.out.println("user name:"+user.get("name")); System.out.println("Gender:"+(user.get("sex") instanceof String ? user.getString("sex"):"")); } }Here are two methods for you
One is to use the getString() method to get the json object value converted from xml, first determine whether the type is string and then obtain it.
One is org.json
You can choose which method to use according to your needs