2015-10-19 58 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和拼合的字符串。 我不是這個的作者,但已經成功地將它用於我的用例。