根据密钥检索数据

时间:2018-07-16 09:25:16

标签: java mongodb

我有一个如下文件

{
    "name" : "Kamala",
    "Condition" : "good",
    "performance" : [ 
        {
            "Date" : "2018-07-18",
            "activity" : 
            {
                "value" : "1",
                "list": "2",
                "cool":"3"
            }
        }, 
        {
            "Date" : "2018-07-12",
            "activity" : 
            {
                "news" : "1",
                "dump": "2"
            }
        }
    ]
}

如何根据键检索"Date" : "2018-07-18"内部的值

1 个答案:

答案 0 :(得分:1)

您可以使用org.json库:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180130</version>
</dependency>

类似的东西:

JSONObject obj = new JSONObject(" .... ");
JSONArray arr = obj.getJSONArray("performance");
for (int i = 0; i < arr.length(); i++)
{
    String date= arr.getJSONObject(i).getString("Date");
    ......
}