将JSON对象列表重构为嵌套列表 [英] Refactor a list of JSON objects to a nested list
本文介绍了将JSON对象列表重构为嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JSON对象数组,我想从中创建一个单个JavaScript对象".我会很欣赏Python或JavaScript中的想法.实际上,我什至不知道用什么恰当的术语来描述(或Google)这种问题.谢谢!
I have an array of JSON objects from which I want to create a "single JavaScript object." I'd appreciate ideas in Python or JavaScript. In fact, I am not even sure what the proper terminology is to describe (or Google) this kind of question. Thanks!
原始列表如下:
[
{
"State_Code": "01",
"County_Code": "000",
"State_Abbrv": "AL",
"County_Name": "ALABAMA",
"DATA": "12345"
},
{
"State_Code": "01",
"County_Code": "001",
"State_Abbrv": "AL",
"County_Name": "AUTAUGA COUNTY",
"DATA": "123"
},
{
"State_Code": "01",
"County_Code": "003",
"State_Abbrv": "AL",
"County_Name": "BALDWIN COUNTY",
"DATA": "321"
},
{
"State_Code": "02",
"County_Code": "000",
"State_Abbrv": "AK",
"County_Name": "ALASKA",
"DATA": "98765"
},
{
"State_Code": "02",
"County_Code": "013",
"State_Abbrv": "AK",
"County_Name": "ALEUTIANS EAST BOROU",
"DATA": "456"
},
.............. ]
我希望它看起来像这样:
And I want it to look like this:
{
"name": "USA",
"children": [
{
"name": "ALABAMA",
"children": [
{
"name": "AUTAUGA COUNTY",
"DATA": 123
},
{
"name": "BALDWIN COUNTY",
"DATA": 321
}
]
},
{
"name": "ALASKA",
"children": [
{
"name": "ALEUTIANS EAST BOROU",
"DATA": 456
}
]
}
.............
]
}
我正在尝试使用类似这种想法的Python循环(对任何错别字表示歉意,今晚将解决):
I am attempting to use a Python loop like this kind of idea (apologies for any typos, will fix tonight):
runningListCounty = []
tempD = defaultdict(dict)
tempDCounty = defaultdict(dict)
i = 0
for l in listOfJson:
if l['County_Code'] == '000'
tempD['name'] = l['County_Name']
if i > 0: #only do this after the first loop
tempD['children'] = runningListCounty
runningList.append(tempD)
runningListCounty = []
tempD = defaultdict(dict)
else:
tempDCounty = defaultdict(dict)
tempDCounty['name'] = l['County_Name']
tempDCounty['DATA'] = l['DATA']
runningListCounty.append(tempDCounty)
i = i + 1
推荐答案
尝试使用猜测功能.
data = [
{
"State_Code": "01",
"County_Code": "000",
"State_Abbrv": "AL",
"County_Name": "ALABAMA",
"DATA": "12345"
},
{
"State_Code": "01",
"County_Code": "001",
"State_Abbrv": "AL",
"County_Name": "AUTAUGA COUNTY",
"DATA": "123"
},
{
"State_Code": "01",
"County_Code": "003",
"State_Abbrv": "AL",
"County_Name": "BALDWIN COUNTY",
"DATA": "321"
},
{
"State_Code": "02",
"County_Code": "000",
"State_Abbrv": "AK",
"County_Name": "ALASKA",
"DATA": "98765"
},
{
"State_Code": "02",
"County_Code": "013",
"State_Abbrv": "AK",
"County_Name": "ALEUTIANS EAST BOROU",
"DATA": "456"
}
]
roots = [ d for d in data if d['County_Code'] == '000']
def crawl(data, roots):
for root in roots:
yield {
'name' : root['County_Name'],
'children' : [{ 'name' : d['County_Name'], 'DATA' : d['DATA'] }
for d in data if d['County_Code'] != '000' and d['State_Code'] == root['State_Code']]
}
final = { 'name' : 'USA', 'children' : [ x for x in crawl(data, roots)]}
import pprint
pprint.pprint(final)
赠予:
{'children': [{'children': [{'DATA': '123', 'name': 'AUTAUGA COUNTY'},
{'DATA': '321', 'name': 'BALDWIN COUNTY'}],
'name': 'ALABAMA'},
{'children': [{'DATA': '456', 'name': 'ALEUTIANS EAST BOROU'}],
'name': 'ALASKA'}],
'name': 'USA'}
这篇关于将JSON对象列表重构为嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文