jsonpath

作者: candice0430 | 来源:发表于2022-11-25 09:48 被阅读0次

一、jsonpath是什么?
二、如何使用?


image.png

三、实践
1.$.filed1.field2

from jsonpath_ng import jsonpath,parse

json_str = {"data":{
        "ecifId":"2022100400000103",
        "operationId":"1628612966502549718749141715287189397281668389298609270",
        "operationStatus":100,
        "pinSetedFlag":"1",
        "securityMethods":[]
    },
    "success":True}

expre_str = parse('$.data.operationStatus')
data = expre_str.find(json_str)
print("operationStatus:",data[0].value)
image.png

2.$.field1[*].id
获取列表中的所有

json_str1 = data = { "store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

expre_str = parse('$.store.book[*].price')
data = expre_str.find(json_str1)
prices = [d.value for d in data]
print(prices)
image.png

相关文章

网友评论

      本文标题:jsonpath

      本文链接:https://www.haomeiwen.com/subject/kzclxdtx.html