繁体   English   中英

使用Java获取JSON键

[英]Fetch the json keys using 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]"
              }
           ]
        }
     ]
  }

试试看...这可能适合您...

您必须使用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...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM