The json-lib package is used to convert the time and space values of xml to json into air brackets. json-libjson
The project database contains a lot of data with a value of []. The test reports a bug. After troubleshooting, it is caused by the use of the json-lib jar package. Json-lib converts a null value to [] when converting an xml string to a json format. Now, let's briefly summarize the similarities and differences between the two types of xml to json packages. It may be because such questions are relatively preliminary, so although some people raise such questions online, no answers are found. I wrote this blog to help beginners like me discover and solve problems as soon as possible.
Most of the xml-to-json jar packages available on the Internet are net. sf. json-lib. This package is widely used in json parsing, but the disadvantage is that many dependent packages are needed. But here we only talk about converting xml to json.
1. Convert net. sf. json-lib to json;
JSON json =xmlSerializer.read(xml);
The problem with this method is that the null value is converted to [] and json is used. get (value ). when getclass gets the [] type, it can be found that json-lib recognizes null values as the jsonarray type rather than the string type, while the tostring method of jsonarray is rewritten to [,]
If you still want to use this method, you can make the following changes. When you use 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. Convert org. json to json:
org.json.JSONObject jsonObj = org.json.XML.toJSONObject(xml);
No [] value is generated, but the root tag is retained (there is only one root tag in the standard XML document, the so-called root tag is a pair of <root tag> </root tag> containing all other tags). Generally, root tags are meaningless for data, so if you need to process them
jsonObj.get("root");
The following is the test code:
Package com.ppt v. 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> </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. the root tag System must be removed from json. 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 "):""));}}
Two methods are provided here.
One is to use the getString () method to obtain the json object value converted from xml, first determine whether the type is string and then obtain
One is org. json.
You can choose which method to use based on your needs.