JAVA JSON parsing: class XPath parsing json

Source: Internet
Author: User
Tags xpath

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.