[英]Nested Dictionary objects json serialization
I am using django rest framework to returned response in json format for jquery. 我正在使用django rest框架以json格式返回jquery的响应。 I have a dictionary object which contains another dictionary object:
我有一个字典对象,其中包含另一个字典对象:
items = {
['name':'Chairs','options':{'type':'office','price':100}],
['name':'Tables','options':{'type':'office','price':45}],
}
response = Response( json.dumps(output_items) , status=status.HTTP_200_OK)
On JavaScript side I am using this code: 在JavaScript方面,我正在使用以下代码:
var array = JSON.parse(json);
that is not parsing JSON, that is creating errors. 这不是解析JSON,而是在创建错误。
I want to create this json format: 我想创建这种json格式:
{ "1": { "name": "Chairs", "description": "All chairs", "options": { "1":{"type": "Office", "price": 130 }, "2":{"type": "Home", "price": 75 }, "3":{"type": "Shop", "price": 100 } } }, "2": { "name": "Tables", "description": "table description", "options": { "1":{"type": "Office", "price": 240 }, "2":{"type": "Home", "price": 200 }, "3":{"type": "Shop", "price": 180 } } } }
I stored all my data using python dictionary and list objects , how can i create this format json output string from dictionaries data? 我使用python字典和列表对象存储了所有数据,如何从字典数据创建此格式的json输出字符串?
You have an error in you object "items". 您的对象“项目”中有错误。 Try this one
试试这个
items = [
{'name':'Chairs','options':{'type':'office','price':100}},
{'name':'Tables','options':{'type':'office','price':45}},
]
You have an error in dict creation ['name':'Chairs','options':{'type':'office','price':100}]
This is not a pair of key: value 您在dict的创建过程中遇到了错误
['name':'Chairs','options':{'type':'office','price':100}]
这不是一对键:value
This is not a proper json or Python object. 这不是正确的json或Python对象。 Python Lists cannot take named arguments.
Python列表不能使用命名参数。 Only a dictionary can take key, value pairs.
只有字典可以接受键,值对。 If you wish to go for a list, it has to be dictionaries added to the list and not as key value pairs.
如果要查找列表,则必须将字典添加到列表中,而不是作为键值对。 The items should be something like this:
这些项目应该是这样的:
List of dictionaries 字典清单
items = [
{"name":"Chairs","options":{"type":"office","price":100}},
{"name":"Tables","options":{"type":"office","price":45}},
]
(or) Dictionary of dictionaries (或)词典字典
items = {
"first":{"name":"Chairs","options":{"type":"office","price":100}},
"second":{"name":"Tables","options":{"type":"office","price":45}}
}
you are not doing it correctly as mentioned by others, you can test it in your browser's console just type in 您没有按照别人的建议正确执行操作,可以在浏览器的控制台中进行测试,只需输入
x={'type':'office','price':100}
//Object {type: "office", price: 100}
y={'type':'office','price':45}
//Object {type: "office", price: 45}
opt1={'type':x}
//Object {type: Object}
opt2={'type':y}
//Object {type: Object}
val1={'name':'Chairs', 'options':opt1}
//Object {name: "Chairs", options: Object}
val2={name:'tables','options':opt2}
//Object {name: "tables", options: Object}
items={'1':val1,'2':val2}
you will have the needed format for your data and you will get an idea about how to formulate the data too. 您将拥有所需的数据格式,并且也将获得有关如何编制数据的想法。 hope it helps
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.