将元素追加到json对象python中 [英] Append element into a json object python
本文介绍了将元素追加到json对象python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Json对象,我每次输入新数字时都试图添加一个新元素.杰森看起来像这样:
I have a Json object and Im trying to add a new element every time I enter a new number. The Json looks like this:
[
{
"range": [1,2,3,4,5]
}
]
这是我的代码:
import json
number = raw_input("enter a number: ")
json_file = 'json.json'
json_data = open(json_file)
data = json.load(json_data)
data.append({"range": number})
print data
例如,如果我的新数字为10,则我希望新的json文档具有:[1, 2, 3, 4, 5, 10
].我通过代码得到的输出是:
If my new number is 10 for example, I want my new json document to have: [1, 2, 3, 4, 5, 10
]. The output I'm getting with my code is:
[{u'range': [1, 2, 3, 4, 5]}, {'range': '25'}]
我正在使用python 2.6
I'm using python 2.6
推荐答案
您需要类似
data[0]['range'].append(10)
或使用int(your_number)
代替10
这篇关于将元素追加到json对象python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文