将多个json对象放入列表

时间:2019-08-02 05:43:23

标签: python json

{"Id": 1,"product": "Mobile","price": 32889,"description": "Samsung"}
{"Id": 2,"product": "laptop","price": 52889,"description": "Dell"}
…
…
{"Id": 3,"product": "Mobile","price": 25889,"description": "Poco"}

每行本身就是一个完整的json对象,具有多行。我正在尝试将这些对象放入列表中。有人可以帮忙吗? 我是Json的新手。

1 个答案:

答案 0 :(得分:0)

如果您的json对象是单独的单独对象,则;

j_obj1={"Id": 1,"product": "Mobile","price": 32889,"description": "Samsung"}
j_obj2={"Id": 2,"product": "laptop","price": 52889,"description": "Dell"}
# init list
my_j_list=[]
# add your objects to list
my_j_list.append(j_obj1)
my_j_list.append(j_obj2)

如果您的json对象是动态的,例如从用户输入中填充

j_obj = <dynamic from outside>
my_j_list=[]
my_j_list.append(j_obj)

Python列表基于索引和值(均值)工作;上面的j_obj_1在索引0处追加,j_obj_2在索引1处追加

要打印列表

# to print glance of list
print my_j_list
# to print individual contents of list, or some operation
for j in my_j_list:
    print j
# to print element stored at index 1
print my_j_list[1]

Python-Doc

了解有关列表的更多信息