Solve the problem that the time-space value of XML to JSON is converted into empty parentheses by using JSON lib package
Many data with the value of brackets [] appear in the project database, and a bug is reported in the test. After troubleshooting, it is caused by the use of JSON lib jar package. In the process of converting XML strings into JSON format, JSON lib will convert null values into []. Now let's briefly summarize the similarities and differences between the two XML to JSON packages. It may be because such questions are relatively elementary, so although some people put forward such questions online, they did not find answers. In order to facilitate junior programmers like me to find and solve problems as soon as possible, I wrote this blog.
Most of the XML to JSON jar packages that can be found on the Internet are net sf. JSON lib, this package is widely used in JSON parsing, but the disadvantage is that it needs more dependent packages. But here we only talk about XML to JSON.
1、net. sf. JSON lib to JSON;
The problem with this method is that it will convert null values to [], using JSON get(value). GetClass gets the type of []. It can be found that JSON lib recognizes null values as jsonarray type instead of 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 judge whether the type is string, and then obtain it.
2、org. JSON to JSON:
The [] value will not be generated, but the root tag will be retained (the standard XML document has and has only one root tag. The so-called root tag is a pair of < root tag > < / root tag > including all other tags). Generally, the root tag is meaningless to the data, so if you need to deal with it
Here is the test code:
Here are two methods for you
One is to use the getString () method to obtain the JSON object value converted from XML, first judge whether the type is string, and then obtain it
One is org json
You can choose which method to use according to your own needs