如何从JSON字符串获取值的所有JSON路径列表?

问题描述 投票:3回答:2

我的目标是读取一个JSON文件并理解所有值的位置,这样当我遇到相同的JSON时,我可以轻松读取所有值。我正在寻找一种方法,以Jayway JsonPath格式返回包含每个数据值的所有路径的列表。

示例JSON:

{
  "shopper": {
    "Id": "4973860941232342",
    "Context": {
      "CollapseOrderItems": false,
      "IsTest": false
    }
  },
  "SelfIdentifiersData": {
    "SelfIdentifierData": [
      {
        "SelfIdentifierType": {
          "SelfIdentifierType": "111"
        }
      },
      {
        "SelfIdentifierType": {
          "SelfIdentifierType": "2222"
        }
      }
    ]
  }
}

理想情况下,我想将JSON作为String,并执行以下操作:

String json = "{'shopper': {'Id': '4973860941232342', 'Context': {'CollapseOrderItems': false, 'IsTest': false } }, 'SelfIdentifiersData': {'SelfIdentifierData': [{'SelfIdentifierType': {'SelfIdentifierType': '111'} }, {'SelfIdentifierType': {'SelfIdentifierType': '2222'} } ] } }";

Configuration conf = Configuration.defaultConfiguration();
List<String> jsonPaths = JsonPath.using(conf).parse(json).read("$");

for (String path : jsonPaths) {
    System.out.println(path);
}

此代码将打印此代码,这是JSON中所有值的位置:

$.shopper.Id
$.shopper.Context.CollapseOrderItems
$.shopper.Context.IsTest
$.SelfIdentifiersData[0].SelfIdentifierData.SelfIdentifierType.SelfIdentifierType
$.SelfIdentifiersData[1].SelfIdentifierData.SelfIdentifierType.SelfIdentifierType

然后理想情况下,我将能够获取该列表并解析相同的JSON对象以获取每个值。

//after list is created
Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);

for (String path : jsonPaths) {
    Object value = JsonPath.read(document, path);
    //do something
}

我知道我可以获得一个代表JSON文件的Map,但我不确定它是否提供了同样的访问权限以检索所有值。如果有一个简单的方法来处理JSONPath,那将是很好的,否则欢迎任何其他方法。

java json hashmap jsonpath
2个回答
12
投票

我提出了一个解决方案,分享以防其他人正在寻找同样的事情:

public class JsonParser {

    private List<String> pathList;
    private String json;

    public JsonParser(String json) {
        this.json = json;
        this.pathList = new ArrayList<String>();
        setJsonPaths(json);
    }

    public List<String> getPathList() {
        return this.pathList;
    }

    private void setJsonPaths(String json) {
        this.pathList = new ArrayList<String>();
        JSONObject object = new JSONObject(json);
        String jsonPath = "$";
        if(json != JSONObject.NULL) {
            readObject(object, jsonPath);
        }   
    }

    private void readObject(JSONObject object, String jsonPath) {
        Iterator<String> keysItr = object.keys();
        String parentPath = jsonPath;
        while(keysItr.hasNext()) {
            String key = keysItr.next();
            Object value = object.get(key);
            jsonPath = parentPath + "." + key;

            if(value instanceof JSONArray) {            
                readArray((JSONArray) value, jsonPath);
            }
            else if(value instanceof JSONObject) {
                readObject((JSONObject) value, jsonPath);
            } else { // is a value
                this.pathList.add(jsonPath);    
            }          
        }  
    }

    private void readArray(JSONArray array, String jsonPath) {      
        String parentPath = jsonPath;
        for(int i = 0; i < array.length(); i++) {
            Object value = array.get(i);        
            jsonPath = parentPath + "[" + i + "]";

            if(value instanceof JSONArray) {
                readArray((JSONArray) value, jsonPath);
            } else if(value instanceof JSONObject) {                
                readObject((JSONObject) value, jsonPath);
            } else { // is a value
                this.pathList.add(jsonPath);
            }       
        }
    }

}

3
投票

请参阅此实用程序:https://github.com/wnameless/json-flattener完美回答您的要求。为复杂的json字符串提供Flattened map和Flattened字符串。我不是这个的作者,但已成功用于我的用例。

© www.soinside.com 2019 - 2024. All rights reserved.