There are many ways to parse JSON in Java, json-lib Ah, Gjson ah, and so on can be parsed, but usually the JSON is converted to an object or a list or map, for us testers, in fact, we do not need all the information inside, in general, We just need to check the inside of a specific value, I heard that Jsonpath can do, I did not carefully read Jsonpath, but, we testers for XPath, should be very familiar with, so, I use the XPath rules, wrote a small application to get JSON value, Here is a concrete introduction to the use of this small application:
The 1.path notation:
JSON, is generally list and map,map say, there is a specific key, but the list only index, so, I design this path, the list with *[index] to replace, as for the map, in the path directly write key on the line, such as/*[1]/ Test, which is a path that represents the 2nd list element in the JSON string in list form, the list element is a map, take the key of which is the value of test, such as the following JSON:
[
{"FirstName": "Eric", "LastName": "Clapton", "instrument": "Guitar"},
{"FirstName": "Sergei", "LastName": "Rachmaninoff", "Instrument": "Piano"}
]
If you want to remove Sergei, the path is written as:/*[1]/firstname
2.jar Package Invocation:
Jsonaction jsonaction = new Jsonaction ();
3. The API provided:
Jsonaction.checkjsonstring (JSON);//Check if the JSON string is legal
Jsonaction.getpathvalue (JSON, path);//core method, gets the value of the given path
Jsonaction.isexistpath (JSON, path);//Check if path exists, or whether it is legal
4.jar Package: (contains the source code)
Http://files.cnblogs.com/files/zhangfei/json-zf-1.0.zip
Welcome to try, such as found any problems, please feel free to contact me, thank you very much!
JAVA JSON parsing: class XPath parsing json