0

我正在使用java并且我导入了

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

假设我有“freeBaggageAvailable”

 "freeBaggageAvailable" : [
    {
        "Y" : [{
                "*" : [{"num":1,"weight": 23,"price": 0}],
               "mc" : [{"num:":1,"weight" : 23,"price": 0}, {"num":2, "weight": 23,"price": 0}],
               "mp" : [{"num:":1,"weight" : 23,"price": 0}, {"num":2, "weight": 23,"price": 0}],
               "mmc" :[{"num:":1,"weight" : 23,"price": 0}, {"num":2, "weight": 32,"price": 0}]
              }],
         "C" : [{
                "*" : [{"num":1,"weight":32 ,"price": 0},{"num":1,"weight":32 ,"price": 0}],
               "mc" : [{"num:":1,"weight" : 32,"price": 0}, {"num":2, "weight": 32,"price": 0},{"num":3,"weight":32 ,"price": 0}],
               "mp" : [{"num:":1,"weight" : 32,"price": 0}, {"num":2, "weight": 32,"price": 0},{"num":3,"weight":32 ,"price": 0}],
               "mmc" :[{"num:":1,"weight" : 23,"price": 0}, {"num":2, "weight": 32,"price": 0}]
              }],

          "F" : [{
                "*" : [{"num":1,"weight":32 ,"price": 0},{"num":1,"weight":32 ,"price": 0}],
               "mc" : [{"num:":1,"weight" : 32,"price": 0}, {"num":2, "weight": 32,"price": 0},{"num":3,"weight":32 ,"price": 0},{"num":4,"weight":32 ,"price": 0}],
               "mp" : [{"num:":1,"weight" : 32,"price": 0}, {"num":2, "weight": 32,"price": 0},{"num":3,"weight":32 ,"price": 0},{"num":4,"weight":32 ,"price": 0}],
               "mmc" :[{"num:":1,"weight" : 23,"price": 0}, {"num":2, "weight": 32,"price": 0},{"num":4,"weight":32 ,"price": 0}]
              }]}
     ],

如果我想访问数据“Y”->“mc”->“num”
如何从 JsonObject 访问它

 File file = new File(classLoader.getResource("files/BaggageInfoPolicy.json").getFile());

        Object obj = parser.parse(new FileReader(file));
        JSONObject jsonObject = (JSONObject) obj;
4

1 回答 1

0

在 JSON.org 参考实现中,您可以执行以下操作:

for (String key : JSONObject.getNames(jsonObject))
{
    map.put(key, jsonObject.get(key));
}

在简单的 JSON 中,你会这样做:

for (Object keyObject : jsonObject.keySet())
{
    String key = (String)keyObject;
    map.put(key, (String)jsonObject.get(key));
}
于 2017-11-29T19:20:47.417 回答