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?