正在转换 JSON 中的 Python 列表
Converting Python list in JSON
我想将 python 列表转换为 json,这是输入
obj=([{'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'}],
[{'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'}])
期望的输出:
{ "file": [
{
"value1": "a",
"value2": None,
"value3": "abc",
"value4": "uyt",
"value5": "tuy",
"value6": None,
"value7": None,
"value8": "a",
},
{
"value1": "b",
"value2": None,
"value3": "abc",
"value4": "xyz",
"value5": "tes",
"value6": None,
"value7": None,
"value8": "b",
}
]
}
PS:我关注了 Whosebug 上的一些现有问题,但它对我不起作用。
谢谢你。 :)
让我们从简单的开始新的一天:-
obj=([{'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'}],
[{'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'}])
D = {'file': [li[0] for li in obj]}
您可以将 defaultdict
用作 list
和 json.dumps
,如下所示:
from collections import defaultdict
obj=(
[
{'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'}
],
[
{'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'}
])
dct = defaultdict(list)
for l in obj:
dct['file'].append((l[0]))
dct_json = json.dumps(dct, indent=2)
print(dct_json)
输出:
{
"file": [
{
"value1": "a",
"value2": null,
"value3": "abc",
"value4": "xyz",
"value5": "tes",
"value6": null,
"value7": null,
"value8": "a"
},
{
"value1": "b",
"value2": null,
"value3": "abc",
"value4": "uyt",
"value5": "tuy",
"value6": null,
"value7": null,
"value8": "b"
}
]
}
这应该适用于任意数量的列表:
import json
obj = (
[{
'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'
}],
[{
'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'
}]
)
result = {'file': []}
for l in obj:
result['file'].append(l[0])
result_json = json.dumps(result, indent=2)
print(result_json)
输出:
{
"file": [
{
"value1": "a",
"value2": null,
"value3": "abc",
"value4": "xyz",
"value5": "tes",
"value6": null,
"value7": null,
"value8": "a"
},
{
"value1": "b",
"value2": null,
"value3": "abc",
"value4": "uyt",
"value5": "tuy",
"value6": null,
"value7": null,
"value8": "b"
}
]
}
有些代码可能是多余的,但我会这样做:
import json
obj=([{'value1': 'a', 'value2': None, 'value3': 'abc'}], [{'value1': 'b', 'value2': None, 'value3': 'abc'}])
obj_list = [i[0] for i in obj]
dict1 = {"file": obj_list}
json_object = json.dumps(dict1, indent=1)
print(json_object)
这是您要查找的代码
import json
obj=([{'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'}],[{'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'}])
file = {"file": []}
file['file'] = [i for i in obj]
print(json.dumps(file, indent=2, sort_keys=True))
您也可以删除 sort_keys 参数,或者如果您不希望它对事物进行排序,则将其设置为 false
PS: 如果你想减少缩进减少 2 到更低的值
我想将 python 列表转换为 json,这是输入
obj=([{'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'}],
[{'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'}])
期望的输出:
{ "file": [
{
"value1": "a",
"value2": None,
"value3": "abc",
"value4": "uyt",
"value5": "tuy",
"value6": None,
"value7": None,
"value8": "a",
},
{
"value1": "b",
"value2": None,
"value3": "abc",
"value4": "xyz",
"value5": "tes",
"value6": None,
"value7": None,
"value8": "b",
}
]
}
PS:我关注了 Whosebug 上的一些现有问题,但它对我不起作用。 谢谢你。 :)
让我们从简单的开始新的一天:-
obj=([{'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'}],
[{'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'}])
D = {'file': [li[0] for li in obj]}
您可以将 defaultdict
用作 list
和 json.dumps
,如下所示:
from collections import defaultdict
obj=(
[
{'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'}
],
[
{'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'}
])
dct = defaultdict(list)
for l in obj:
dct['file'].append((l[0]))
dct_json = json.dumps(dct, indent=2)
print(dct_json)
输出:
{
"file": [
{
"value1": "a",
"value2": null,
"value3": "abc",
"value4": "xyz",
"value5": "tes",
"value6": null,
"value7": null,
"value8": "a"
},
{
"value1": "b",
"value2": null,
"value3": "abc",
"value4": "uyt",
"value5": "tuy",
"value6": null,
"value7": null,
"value8": "b"
}
]
}
这应该适用于任意数量的列表:
import json
obj = (
[{
'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'
}],
[{
'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'
}]
)
result = {'file': []}
for l in obj:
result['file'].append(l[0])
result_json = json.dumps(result, indent=2)
print(result_json)
输出:
{
"file": [
{
"value1": "a",
"value2": null,
"value3": "abc",
"value4": "xyz",
"value5": "tes",
"value6": null,
"value7": null,
"value8": "a"
},
{
"value1": "b",
"value2": null,
"value3": "abc",
"value4": "uyt",
"value5": "tuy",
"value6": null,
"value7": null,
"value8": "b"
}
]
}
有些代码可能是多余的,但我会这样做:
import json
obj=([{'value1': 'a', 'value2': None, 'value3': 'abc'}], [{'value1': 'b', 'value2': None, 'value3': 'abc'}])
obj_list = [i[0] for i in obj]
dict1 = {"file": obj_list}
json_object = json.dumps(dict1, indent=1)
print(json_object)
这是您要查找的代码
import json
obj=([{'value1': 'a', 'value2': None, 'value3': 'abc', 'value4': 'xyz', 'value5': 'tes', 'value6': None, 'value7': None, 'value8': 'a'}],[{'value1': 'b', 'value2': None, 'value3': 'abc', 'value4': 'uyt', 'value5': 'tuy', 'value6': None, 'value7': None, 'value8': 'b'}])
file = {"file": []}
file['file'] = [i for i in obj]
print(json.dumps(file, indent=2, sort_keys=True))
您也可以删除 sort_keys 参数,或者如果您不希望它对事物进行排序,则将其设置为 false
PS: 如果你想减少缩进减少 2 到更低的值