How do I parse the JSON string returned by the interface with Java syntax?

Source: Internet
Author: User
Tags string format

Cause: When doing interface testing, it is often necessary to parse the response data returned by the interface (usually JSON string format)

The following is an example of an interface response data:

The following is a parse of the JSON format string returned by the above interface

Object dataobj=jsonpath.read (content, "$.data");        Jsonobject Datajsonobj=json.parseobject (dataobj.tostring ());        Gets the value of key= "SearchResult", which is a JSON array        jsonarray searchresultarray=datajsonobj.getjsonarray ("SearchResult");        Determine if the number of elements in the SearchResult is greater than or equal to 1        if (searchresultarray.size () <1) {               //Error        }

Get searchresult[0]
Object searchresult1=searchresultarray.get (0);
Jsonobject Searchresult1jsonobj=json.parseobject (searchresult1.tostring ());

Jsonarray Itemsarray=searchresult1jsonobj.getjsonarray ("items");
Determines whether the number of items is greater than or equal to 1
if (Itemsarray.size () <1) {
Error
}
Object items0=itemsarray.get (0);
Object Businessobject=jsonpath.read (items0.tostring (), "$.business");
Jsonobject Businessjsonobj=json.parseobject (businessobject.tostring ());
String Hasads=jsonpath.read (businessjsonobj.tostring (), "$.hasads"). ToString ();
Determine the value of a hasads
if (Hasads.equals ("false")) {
//...
}

  

How do I parse the JSON string returned by the interface with Java syntax?

Related Article

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.