[英]How do I convert a string (which is like json) to a json object?
if(i!=0):
json_str+=str({"tag_namespace": "dba_inops", "tag_key": "db_schema", "tag_value": "" + schema_arr.pop(i) + ""},)+","
machine_arr.pop(i);
#print json_str
else:
json_str+=str({"tag_namespace": "dba_inops", "tag_key": "db_schema", "tag_value": "" + schema_arr.pop(i) + ""})
new_machine=machine_arr.pop(i);
print json_str;
SO OUTPUT json_string is like : 所以输出json_string就像这样:
{'tag_key': 'db_schema', 'tag_namespace': 'dba_inops', 'tag_value': 'xyz'}
BUT :- 但是:-
new_tagmap.map=[json_str]
print new_tagmap.map;
when I try to put that constructed json_str into an array I get double quotes after the array bracket which is an invalid json. 当我尝试将构造的json_str放入数组中时,在无效的json数组括号后得到双引号。
["{'tag_key': 'db_schema', 'tag_namespace': 'dba_inops', 'tag_value': 'xyz'}"]
and if I do a replace of first and last char it replaces the {. 如果我替换第一个和最后一个字符,它将替换{。
Is there a way to convert such a string to json, so that I can directly load it into the array as a json object without the "". 有没有一种方法可以将这样的字符串转换为json,这样我就可以将它作为json对象直接加载到数组中,而无需使用“”。
Still not able to add some thing like this :- 仍然无法添加这样的东西:
{'tag_namespace': 'dba_inops', 'tag_key': 'db_name', 'tag_value': 'hi' },
{'tag_namespace': 'dba_inops', 'tag_key': 'db_name', 'tag_value': 'abc' }
it says 它说
raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 1 column 77 - line 1 column 78 (char 77 - 78) bcoz of the , between the two lists when I do a loads(). 引发ValueError(errmsg(“ Extra data”,s,end,len(s)))ValueError:额外数据:当我在两个列表之间时,第1行第77列-第1行第78列(字符77-78)的bcoz做一个loads()。
I recommend using Python's built in json parser. 我建议使用Python内置的json解析器。 Here is the documentation for it .
这是它的文档 。 You can use
json.loads
to create the structure, and use json.dumps
to decode it. 您可以使用
json.loads
创建结构,并使用json.dumps
进行解码。 The link has many easy-to-follow examples. 该链接包含许多易于遵循的示例。
If you want to add or change the object then you should do so in its Python representation. 如果要添加或更改对象,则应以其Python表示形式进行。 So, for example, let's say I load the following JSON string:
因此,例如,假设我加载了以下JSON字符串:
>>> x = json.loads('{ "animals": ["cat", "dog", "fish"], "numbers": [1, 2, 3], "people": ["joe", "sally", "beth"] }')
It creates a Python dictionary. 它创建一个Python字典。 Each entry in the dictionary contains a list of things:
字典中的每个条目都包含以下内容:
>>> x
{u'animals': [u'cat', u'dog', u'fish'],
u'numbers': [1, 2, 3],
u'people': [u'joe', u'sally', u'beth']}
Now let's imagine I want to add "mouse" to the list of animals. 现在,让我们想象一下我想在动物列表中添加“鼠标”。 I can do so with Python's list append function, directly using Python code.
我可以直接使用Python代码使用Python的列表追加功能来实现。 No string manipulation required!
无需字符串操作! This is much nicer and cleaner.
这是更好,更清洁的方法。
x['animals'].append("mouse")
Now let's turn that Python object back into JSON. 现在,让我们将该Python对象转换回JSON。 Behold!
看哪!
>>> my_new_json_string = json.dumps(x)
>>> print my_new_json_string
{"animals": ["cat", "dog", "fish", "mouse"], "numbers": [1, 2, 3], "people": ["joe", "sally", "beth"]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.