如何从Falcon中的POST请求读取原始json请求

2024-04-24 14:30:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我如何解析JSON请求呢?在

{
  "location_with_names": [
    {
      "location_id": 101,
      "names": [
        "a",
        "b",
        "c"
      ]
    },
    {
      "location_id": 102,
      "names": [
        "a",
        "e"
      ]
    },
    {
      "location_id": 103,
      "names": [
        "f",
        "c"
      ]
    }
  ]
}

样本代码:

^{pr2}$

location_with_names为无


Tags: 代码idjsonnameswithlocation样本pr2
1条回答
网友
1楼 · 发布于 2024-04-24 14:30:49

您必须首先反序列化它,然后才能查询它。您正在使用的函数用于else entirely。使用Request对象、bounded或{a3}上可用的stream选项。在

import json

def on_post(self, req, resp):
    raw_data = json.load(req.bounded_stream)
    location_with_names = raw_data.get('location_with_names')
    print(location_with_names)

相关问题 更多 >