2017-02-19 26 views
0
{ 
    "transaction": { 
    "id": 1, 
    "empid": "12345", 
    "details1": { 
     "name": "xyz", 
     "age": "30", 
     "sex": "M", 
     "Address": { 
     "Office": "office", 
     "Home": "Home" 
     } 
    }, 
    "abcDetails": "asdf", 
    "mobile": 123455 
    }, 
    "details2": { 
    "id": 2, 
    "empid": "64848", 
    "details": { 
     "name": "eryje", 
     "age": 3027, 
     "sex": "M", 
     "Address": { 
     "Office": "office", 
     "Home": "Home" 
     } 
    }, 
    "abcDetails": "fhkdl", 
    "mobile": 389928 
    } 
} 

私は上記の形式でデータを取得しています。ここでは分割し、ループを使用してデータを反復処理しました。最初にフォーマットされたデータの下に取得しています。だからこれで私は名前と年齢の値とdetails1.Address.Office値を取得したい(キーは静的ではない)。Javaを使用したネストされたJSON解析

"details1": { 
     "name": "xyz", 
     "age": "30", 
     "sex": "M", 
     "Address": { 
      "Office": "office", 
      "Home": "Home" 
     } 
    } 

答えて

1

JSONObjectキー()を使用してキーを取得し、各キーを反復して動的な値にします。

// searchResult refers to the current element in the array "search_result" 
JSONObject questionMark = searchResult.getJSONObject("question_mark"); 
Iterator keys = questionMark.keys(); 

while(keys.hasNext()) { 
    // loop to get the dynamic key 
    String currentDynamicKey = (String)keys.next(); 

    // get the value of the dynamic key 
    JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey); 

    // do something here with the value... 
} 

参考:How to parse a dynamic JSON key in a Nested JSON result?

関連する問題