gpt4 book ai didi

python - 如何在 Django View 中解析 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:22 25 4
gpt4 key购买 nike

我将一些 JSON 发布到 View 。我现在想解析数据并将其添加到我的数据库中。

我需要获取属性 nametheme 并遍历数组 pages。我的 JSON 如下:

{
"name": "xaAX",
"logo": "",
"theme": "b",
"fullSiteLink": "http://www.hello.com",
"pages": [
{
"id": "1364484811734",
"name": "Page Name",
"type": "basic",
"components": {
"img": "",
"text": ""
}
},

{
"name": "Twitter",
"type": "twitter",
"components": {
"twitter": {
"twitter-username": "zzzz"
}
}
}
]
}

这是我目前所拥有的:

def smartpage_create_ajax(request):

if request.POST:

# get stuff and loop over each page?

return HttpResponse('done')

最佳答案

python 提供json编码/解码 json

import json
json_dict = json.loads(request.POST['your_json_data'])
json_dict['pages']

[
{
"id": "1364484811734",
"name": "Page Name",
"type": "basic",
"components": {
"img": "",
"text": ""
}
},

{
"name": "Twitter",
"type": "twitter",
"components": {
"twitter": {
"twitter-username": "zzzz"
}
}
},

}
]

关于python - 如何在 Django View 中解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15687304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com