【问题标题】:Searching in a complex json information in an tinyDB在 tinyDB 中搜索复杂的 json 信息
【发布时间】:2021-03-23 18:05:15
【问题描述】:

我在 tinyDB 中有以下 json 信息

{
"_default": {
    "1": {
        "status": {
            "timestamp": "2021-03-21T15:12:04.025Z",
            "total_count": 4436
        },
        "data": [
            {
                "id": 1,
                "name": "abc"
            },
            {
                "id": 2,
                "name": "def"
            },
            {
                "id": 1,
                "name": "qwe"
            }
        ]
    }
}

}

我了解如何在状态部分进行搜索,即

listings = db.table('_default')
E = Query()
print(listings.search(E.status.total_count == 4436))

但是如何使用 [] 在数据部分中搜索?

【问题讨论】:

    标签: python json tinydb


    【解决方案1】:

    您可以将其加载为字符串并按索引对其进行解析,因为 data 是一个列表。因此,只需将 [0] 更改为您有兴趣在数据片段中看到的任何索引

    import json
    
    x = """{
       "_default":{
          "1":{
             "status":{
                "timestamp":"2021-03-21T15:12:04.025Z",
                "total_count":4436
             },
             "data":[
                {
                   "id":1,
                   "name":"abc"
                },
                {
                   "id":2,
                   "name":"def"
                },
                {
                   "id":1,
                   "name":"qwe"
                }
             ]
          }
       }
    }"""
    
    y = json.loads(x)
    
    
    print(y["_default"]["1"]["data"][0])
    

    结果:

    {'id': 1, 'name': 'abc'}
    

    编辑:我看到你正在为此使用数据库表,所以你它不会完全适用,但你至少可以通过索引想法获得解析

    【讨论】:

      猜你喜欢
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多