我正在尝试创建一个嵌套的 Json 结构,如下所示:
Json 示例:
{
"id" : "de",
"Key" : "1234567",
"from" : "[email protected]",
"expires" : "2018-04-25 18:45:48.3166159",
"command" : "method.exec",
"params" : {
"method" : "cmd",
"Key" : "default",
"params" : {
"command" : "testing 23"
}
}
我正在尝试从 OrderedDict 中执行此操作。我不确定构造 OrderedDict 以便生成正确的 Json 的正确方法。
Python代码:
json_payload = OrderedDict(
[('id', id),
('Key', keystore),
('from', '[email protected]'),
('expires', expires),
('command', 'method.exec')]
# What goes here for the params section??
)
print json.dumps(json_payload, indent=4, default=str)
使用 @haifzhan 的输出作为输入,准确地交付了所需的内容。
payload = OrderedDict(
[
('id', 'de'),
('Key', '1234567'),
('from', '[email protected]'),
('expires', '2018-04-25 18:45:48.3166159'),
('command', 'method.exec'),
('params',
OrderedDict(
[
('method', 'cmd'),
('Key', 'default'),
('params',
OrderedDict(
[
('command', 'testing 23')
]
)
)
]
)
)
]
)
print json.dumps(json_payload, indent=4, default=str)
您在 JSON 数据末尾错过了
}
。
import json
import collections
data = {
"id" : "de",
"Key" : "1234567",
"from" : "[email protected]",
"expires" : "2018-04-25 18:45:48.3166159",
"command" : "method.exec",
"params" : {
"method" : "cmd",
"Key" : "default",
"params" : {
"command" : "testing 23"
}
}}
data_str = json.dumps(data)
result = json.loads(data_str, object_pairs_hook=collections.OrderedDict)
print(result)
输出:
OrderedDict(
[
('id', 'de'),
('Key', '1234567'),
('from', '[email protected]'),
('expires', '2018-04-25 18:45:48.3166159'),
('command', 'method.exec'),
('params',
OrderedDict(
[
('method', 'cmd'),
('Key', 'default'),
('params',
OrderedDict(
[
('command', 'testing 23')
]
)
)
]
)
)
]
)
一些事情。
id
是一个关键字。您可以只传递字典作为参数。
ids = "de"
keystore = "1234567"
expires = "2018-04-25 18:45:48.3166159"
pdict = {
"method" : "cmd",
"Key" : "default",
"params" : {
"command" : "testing 23"
}
}
json_payload = OrderedDict(
[('id', id),
('Key', keystore),
('from', '[email protected]'),
('expires', expires),
('command', 'method.exec'),
('params',pdict )
]
)
print(json.dumps(json_payload, indent=4, default=str))
工作中!!!
主要是当我们序列化查询集而不是模型实例时
例如:
serialized_data = SnippetSerializer(MyModel.objects.all(), many=True)
输出:
[
OrderedDict([('code', 'ABC'), ('quantity', 5.0)]),
OrderedDict([('code', 'GGG'), ('quantity', 4.0)])
]
我们可以将其转换为 json,如下所示:-
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
import io
result = JSONRenderer().render(serialized_data)
output_stream = io.BytesIO(result)
data = JSONParser().parse(output_stream)
print(data)
输出:
[
{'code': 'ABC', 'quantity': 5.0},
{'code': 'GGG', 'quantity': 4.0}
]
感谢 @Nids Barthwal 的回答,我能够绕过 json_script 模板标签的限制。我使用 JSONRenderer 策略来创建自定义标签:
@register.filter(name='ordereddict_to_json')
def ordereddict_to_json(value):
result = JSONRenderer().render(value)
output_stream = io.BytesIO(result)
data = JSONParser().parse(output_stream)
return json.dumps(data)
...一旦我将其插入数据属性...
<div id="foo" data-datavalue='{{ contextThing|ordereddict_to_json }}'>
// Something dynamically rendered in javascript
</div>
...jQuery 很高兴在前端接手:
function renderMyThing(thing) {
let dataValue = thing.data("datavalue")
let htmlString = `This part: ${dataValue.something} and also this other part: ${dataValue.somethingElse}`
// ...
}