2015-10-19 56 views
1

我的目标是读取JSON文件并理解所有值的位置,以便当我遇到相同的JSON时,可以轻松读取所有值。我正在寻找一种方法来返回一个列表,其中包含所有到每个数据值的路径,格式为Jayway JsonPath如何从JSON字符串中获取所有JSON路径的值列表?

例JSON:

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

理想我想采取JSON作为一个字符串,做这样的事情:

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 

那么理想情况下,我将能够获取该列表并解析th使用相同的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文件的表示,但我不知道提供相同四通八达的交通网络,以获取所有值。如果使用JSONPath有一个简单的方法,那很好,否则任何其他方法都是可以的。

+0

使用任何json解析和一点递归它似乎不是很复杂 – njzk2

回答

8

我想出了一个解决方案,共享的情况下,任何人都在寻找同样的事情:

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); 
      }  
     } 
    } 

} 
0

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