2014-11-21 102 views
0

我有一个json对象我如何获得所有密钥,之后又没有对密钥进行硬编码如何获取密钥值。使用java获取json密钥

{ 
    "A":"M1", 
    "Data":[ 
     { 
      "B":[ 
       { 
       "B1":"111", 
       "B2":"Warning " 

       }, 
       { 
       "B1":"222", 
       "B2":"Warning " 

       } 
      ], 
      "C":[ 
       { 
       "c1":"IL2", 
       "c2":"[0.750183,0.00933380975964486]" 
       }, 
       { 
       "c1":"IL1b", 
       "c2":"[0.750183,-1.5216938335421]" 
       } 
      ] 
     } 
    ] 
    } 
+3

可能重复[如何解析Java中的JSON(http://stackoverflow.com/questions/2591098/how-to-parse-json-in -java) – Manwal 2014-11-21 04:40:29

+0

请参考下面的链接,可以帮助你 [访问JSON对象的元素不知道这些按键的名称] [1] [1]:http://stackoverflow.com/questions/ 5113847/json-objects-accessible-elements-of-know-the-key-names – ABI 2014-11-21 04:43:57

+0

jack儿子真的很好处理json字符串。它允许直接在pojo或任何期望的对象中解析json。它有很大的灵活性来处理 json。 请参阅http://jackson.codehaus.org/了解更多详情。 – Gaurav 2014-11-21 05:03:10

回答

0

尝试了这一点... 这可能会为你工作....

你必须使用的JSONObject keys()拿到钥匙,然后遍历每个键进入动态值。

大致代码如下:

// 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... 
} 
+0

我正在使用import org.json.JSONObject;是我可以使用的罚款或任何其他罐子 – 1209 2014-11-21 05:13:57