json 如何按顺序取出[已关闭]

1dkrff03  于 2023-02-26  发布在  其他
关注(0)|答案(2)|浏览(90)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

3天前关闭。
Improve this question
这是我在json中的示例数据,我想使用javascript map函数来实现这一点。

data = [{
  campaign_name: 'Song Khran',
  items: [{
   title: 'SK_Item 1',
   shop_name: 'Shop1'
},{
   title: 'SK_Item 2',
   shop_name: 'Shop2'
},{
   title: 'SK_Item 3',
   shop_name: 'Shop3'
}]
},{
  campaign_name: 'Thadin Gyut',
  items: [{
   title: 'TG_Item 1',
   shop_name: 'Shop4'
},{
   title: 'TG_Item 2',
   shop_name: 'Shop5'
},{
   title: 'TG_Item 3',
   shop_name: 'Shop6'
}]
}]

像这样打印出来
宋卡兰= SK_项目1、SK_项目2、SK_项目3
Thadin Gyut = TG_项目1、TG_项目2、TG_项目3

vq8itlhq

vq8itlhq1#

你可以试试看:

const data = [
    {
        campaign_name: 'Song Khran',
        items: [
            {
                title: 'SK_Item 1',
                shop_name: 'Shop1'
            },
            {
                title: 'SK_Item 2',
                shop_name: 'Shop2'
            },
            {
                title: 'SK_Item 3',
                shop_name: 'Shop3'
            }
        ]
    },
    {
        campaign_name: 'Thadin Gyut',
        items: [
            {
                title: 'TG_Item 1',
                shop_name: 'Shop4'
            },
            {
                title: 'TG_Item 2',
                shop_name: 'Shop5'
            },
            {
                title: 'TG_Item 3',
                shop_name: 'Shop6'
            }
        ]
    }
];

const a = data.map(function(e) {
    return {
        name: e.campaign_name,
        title: Object.values(e)[1].map(v => v.title),
    };
});

for (let v of a) {
    console.log(`${v.name} = ${v.title.join(',')}`);
}
kadbb459

kadbb4592#

请将列表和字典中的所有值都设置为字符串值,这样问题就完美了。不过,我有一个可以帮助您的解决方案。我也有一个JavaScript的解决方案:

data = [{ 'campaign_name': 'Song Khran', 'items': [{ 'title': 'SK_Item 1', 'shop_name': 'Shop1' },{ 'title': 'SK_Item 2', 'shop_name': 'Shop2' },{ 'title': 'SK_Item 3', 'shop_name': 'Shop3' }] },{ 'campaign_name': 'Thadin Gyut', 'items': [{ 'title': 'TG_Item 1', 'shop_name': 'Shop4' },{ 'title': 'TG_Item 2', 'shop_name': 'Shop5' },{ 'title': 'TG_Item 3', 'shop_name': 'Shop6' }] }]


for (i = 0; i < data.length; i++) {
    campName = data[i]['campaign_name'];
    itemList = data[i]['items'];
    itemData = '';
    for (j = 0; j < itemList.length; j++){
         itemData += itemList[j]['title']+','
     }
    console.log( campName +'='+itemData )
}

此解决方案适用于Python

data = [{ 'campaign_name': 'Song Khran', 'items': [{ 'title': 'SK_Item 1', 'shop_name': 'Shop1' },{ 'title': 'SK_Item 2', 'shop_name': 'Shop2' },{ 'title': 'SK_Item 3', 'shop_name': 'Shop3' }] },{ 'campaign_name': 'Thadin Gyut', 'items': [{ 'title': 'TG_Item 1', 'shop_name': 'Shop4' },{ 'title': 'TG_Item 2', 'shop_name': 'Shop5' },{ 'title': 'TG_Item 3', 'shop_name': 'Shop6' }] }]

i=0
for item in data:
    campName = data[i]['campaign_name']
    itemList = data[i]['items']
    itemData = ''
    for item in itemList:
        itemData += ' ' + item['title']
    print( campName +'='+ itemData )
    i+=1

输出:

Song Khran= SK_Item 1 SK_Item 2 SK_Item 3
Thadin Gyut= TG_Item 1 TG_Item 2 TG_Item 3

相关问题