我从查询中得到以下输出(它有3条记录) 在3条记录中,第一条和第三条在数据数组中具有值。第二个是空的。 我想遍历完整的json并获取所有嵌套值,即为此的数据数组内部 特定记录。
下面给出了Json结构以及我试图编写的代码片段 值也被写入。我试图遍历嵌套数组并将值设置为 对象,然后将该对象附加到数组,最后再次将此数组设置为属性 父对象的但是,我无法获取嵌套数组的所有值。
当第一个for循环正常工作时,我正在遵循类似的技术来获取嵌套循环的详细信息。
data":[
{
"main":{
"responseDetails":{
"limit":250,
"offset":0,
"size":12,
"total":12
},
"data":[
{
"module1":"243",
"module2":"abc",
"module3":"in",
"module4":"yes"
},
{
"module1":"133",
"module2":"ac",
"module3":"in",
"module4":"yes"
},
{
"module1":"123",
"module2":"bcc",
"module3":"in",
"module4":"yes"
}
]
},
"name":null,
"gender":"female",
"visit":"today" ,
"visit2":"tomorrow"
},
{
"main"{
"responseDetails":{
"limit":250,
"offset":0,
"size":0,
"total":0
},
"data":[
]
},
"name":null,
"gender":"female",
"visit":"today",
"visit2":"tomorrow"
},
{
"main"{
"responseDetails":{
"limit":250,
"offset":0,
"size":10,
"total":10
},
"data":[
{
"module1":"BMW",
"module2":"abc",
"module3":"in",
"module4":"yes"
},
{
"module1":"dwbb",
"module2":"abc",
"module3":"in",
"module4":"yes"
},
{
"module1":"CFP",
"module2":"abc",
"module3":"in",
"module4":"yes"
}
]
},
"name":null,
"gender":"female",
"visit":"today",
"visit2":"tomorrow"
}
]
}
我的代码如下,我希望将嵌套值作为响应的一部分返回
for data_response in my_response:
arr = []
tem_arr=[]
full_arr =[]
for subitem in data_response['data']:
temp_store = {}
temp_store['name'] = subitem['name']
temp_store['gender'] = subitem['gender']
temp_store['visit'] = subitem['visit']
temp_store['visit2'] = subitem['visit2']
arr = subitem['main']['data']
for item_o in arr:
if(arr is not None):
temp_substore = {}
temp_substore['module1']=item_o['module1']
temp_substore['module2']=item_o['module2']
temp_arr.append(temp_substore)
temp_store['subval'] = temp_substore
full_arr.append(temp_store)
我将返回full_arr
作为响应,但输出如下:
response
{name:null, gender:'female', visit:'today',visit':tomorrow', subvalue:{module1:123,module2:bcc,module3:in, module4:'yes'} # takes only last value of nested node of 1st record
# {second record is ok}
{name:null, gender:'female', visit:'today',visit':tomorrow', subvalue:{module1:CFP,module2:abc,module3:in, module4:'yes'} # last value of nested node of 3rd record.
我想要所有记录的嵌套值。