JSONPath parsing json

  • JSONPath - XPath for the JSON

  • For parsing a nested json data; JsonPath library is an information extraction it is to extract information from the designated tool JSON document.

      Considering next plans to develop an automated test platform, the interface test sets several checkpoints, then think of using JSONPath check to do the checkpoint.

import com.alibaba.fastjson.JSONPath;
import java.util.HashMap;
import java.util.Map;

public class JSONPathDemo {

    public static void main(String[] args) {

        String json = "{\"store\":{\"book\":[{\"title\":\"高效Java\",\"price\":10.2},{\"title\":\"设计模式\",\"price\":12.2},{\"title\":\"重构\",\"isbn\":\"553\",\"price\":8},{\"title\":\"虚拟机\",\"isbn\":\"395\",\"price\":22}],\"bicycle\":{\"color\":\"red\",\"price\":19}}}";
        //String expression1  = (String) JSONPath.read(json,"$.store.book[0].title");
        //int expression2 = (int) JSONPath.read(json,"$.store.book[0].price");

        //Checkpointed, among a plurality of check points separated by; 
        String params = "$ .store.book [0 ] .title = efficiently the Java;. $ Store.book [0] = 10.2 .price" ; 
        String [] Data = params.split ( ";" ); 
// iterate, each acquiring a checkpoint in the corresponding data JSON the Map <String, Object> = Map new new the HashMap <> (); for ( int I = 0; I <data .length; I ++ ) { // System.out.println (Data [I]); map.put (Data [I] .split ( "=") [0], Data [I] .split ( "=") [. 1 ]); System.out.println ( "return data JSONPath:" + JSONPath.read (JSON, data [I] .split ( "=") [0 ])); System.out.println ("Predicate data JSONPath:" + as map.get (Data [I] .split ( "=") [0 ])); // determine whether checkpoint data and return the data match json IF (JSONPath.read (json, Data [I] .split ( "=") [0]) the instanceof String) { IF (JSONPath.read (JSON, Data [I] .split ( "=") [0]). the equals (as map.get (Data [ I] .split ( "=") [0 ]))) { System.out.println ( "Pass A" ); } the else { System.out.println ( "the Fail A" ); } } the else { // Object turn String IF ((JSONPath.read(json,data[i].split("=")[0]).toString()).equals((map.get(data[i].split("=")[0])))){ System.out.println("Pass B"); }else { System.out.println("Fail B"); } } } } }

 

 

Guess you like

Origin www.cnblogs.com/wakey/p/11760471.html