简体   繁体   中英

Jsonpath always returning empty list

I am trying to fetch the values from json file with the help of jayway JsonPath. But everytime it returns empty list.

I am trying to use the json path as *.singleAccomViewData

import java.util.*;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;

public class JSONMapper {
  public static void main(String[] args) throws Exception {
    DocumentContext jsonContext = JsonPath.parse("D:\\Docs\\search.json");
     List<String> accom = JsonPath.read("D:\\Docs\\search.json", "*.singleAccomViewData");
     System.out.println("accom value: " + accom);
  }
}

Below is my JSON File:

{
  "searchResult": {
    "singleAccomViewData": null,
    "singleAccomSearch": false,
    "durationSelection": {
      "defaultDisplay": [
        6,
        7,
        8,
        9,
        10
      ] 
    }
   }
}

There are a few reasons why the above is not working:

  1. If you are calling JsonPath.read(String, String) then the first String is expected to be the Json itself, not a path to a Json file.

  2. Using * for the root of your JsonPath is not valid, it should be $ .

  3. There is another object above what you are looking for: searchResult .

Your final JsonPath should be $.searchResult.singleAccomViewData . Fix the above issues and it should work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM