将 json 转换为格式化字符串
2020-5-22 14:43:4
收藏:0
阅读:182
评论:2
我想将此json转换为:
{
"rate_limit_by":
[{ "type": "IP",
"extract_from_header": "X-Forwarded-For"
}]
}
成这个:
"{\"rate_limit_by\": [{\"type\": \"IP\", \"extract_from_header\": \"X-Forwarded-For\"}]}".
以便我可以将其作为Python请求中的负载部分发送。
我已经尝试了多种方法。json.dumps不起作用,因为在这种情况下它不会转义字符。&.replace(""",r"\"")不起作用,因为它会创建类似于以下字符串:
{\\"rate_limit_by\\": [{\\"type\\": \\"IP\\", \\"extract_from_header\\": \\"X-Forwarded-For\\"}]}
(下面是curl的示例,但我想使用Python请求以特定格式发送数据。) 我的上游希望以某种格式接收数据,目前我将数据发送到上游如下:
curl -i --request POST --data "rule_name=only_ip" \
--data-binary "@data.txt" \
--url http://localhost:8001/plugin/rules
data.txt看起来像这样:
rule={
"rate_limit_by": [
{ "type":"IP", "extract_from_header": "X-Forwarded-For" }
]
}
我试图将其转换为:
curl -i --request POST -H 'Content-Type: application/json' --data-binary @data.json http://localhost:8001/plugin/rules
data.json应如下所示
{
"rule_name" : "test_ip",
"rule":"{\"rate_limit_by\": [{\"type\": \"IP\", \"extract_from_header\": \"X-Forwarded-For\"}]}"
}
现在"rule"的值是带有字符转义的字符串。 我尝试使用python发布,以下是相同的代码:
import requests
import json
import re
url = 'http://localhost:8001/plugin/rules'
rule = {
"rate_limit_by":
[{ "type": "IP",
"extract_from_header": "X-Forwarded-For"
}]
}
rule = json.dumps(json.dumps(rule))
print(rule) #这会以正确的格式输出数据
obj = {
"rule_name" : "test_ip",
"rule": rule #但是当在这里使用它时,它会被包装在两个\\中
}
headers = {'Content-Type': 'application/json', 'Accept': 'text/plain'}
print(obj)
r = requests.post(url, data=obj, headers=headers)
print(r.text)
点赞
用户88888888
desired 是你在 something.json 文件中需要的内容。下面的代码打印出 True。请查看 https://repl.it/repls/DistantTeemingProtocol。
import json
desired = r'''{
"rule_name" : "test_ip",
"rule":"{\"rate_limit_by\": [{\"type\": \"IP\", \"extract_from_header\": \"X-Forwarded-For\"}]}"
}'''
d = {
"rate_limit_by": [{
"type": "IP",
"extract_from_header": "X-Forwarded-For"
}]
}
s = json.dumps(d)
xxx = json.dumps({"rule_name": "test_ip", "rule": s}, indent=4)
o = json.loads(desired)
yyy = json.dumps(o, indent=4)
print(xxx == yyy)
如果你想使用 requests 进行 POST 请求,那么你应该使用字典而不是字符串进行 POST。
例如,
r = requests.post(url, json={"rule_name": "test_ip", "rule": s})
2020-05-22 14:22:46
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

你的意思是你想以某种方式访问里面的项目吗?
你应该去掉“[]”,因为那部分没有什么意义。
import json x = str({ "rate_limit_by": [{ "type": "IP", "extract_from_header": "X-Forwarded-For" }] }) x = x.replace("[","") x = x.replace("]","") x = eval(x) d = json.dumps(x) l = json.loads(d) l['rate_limit_by']['type']这会输出“IP”。现在你拥有了一个叫做 l 的你所需要的字典。