首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据实际数据和预期数据重构json

根据实际数据和预期数据重构json
EN

Stack Overflow用户
提问于 2019-07-09 06:35:02
回答 1查看 61关注 0票数 0

我想根据原始的json数据和预期的json数据来重构json。

如果仔细查看原始的json数据,我会发现除了男性/女性属性之外还有其他国家/地区。我希望国家/地区模块位于基于国家/地区模块内的方向属性的男性/女性属性内。因此,在后续数据中,我将在男性属性中有1个国家模块(因为有1个男性记录),在女性属性中有2个国家模块(因为有2个女性记录)。

原始json数据如下所示:

代码语言:javascript
复制
{
  "Implementations": [
    {      
      "Male": {
        "Gender": "Male"              
      },
      "Female": {
        "Gender": "Female"       
      },

      "Country": [
        {
          "Orientation": "Male",          
          "Name": ABCD
        },
        {
          "Orientation": "Female",
          "Name": EFGH
        },
        {
          "Orientation": "Female",
          "Name": IJKL        
        }
      ],
      "State": [
        {          
          "Address": "XYZ Street",
          "ZipCode": "US"
        }
      ]
    }
  ]
}

期望的json数据:

代码语言:javascript
复制
{ 
  "Implementations":    [
    {      
      "Male": {
        "Gender": "Male"         
         "Country": [
                {
                  "Orientation": "Male",          
                  "Name": ABCD
                }
            ],
              "State": [
                {          
                  "Address": "XYZ Street",
                  "ZipCode": "US"
                }
            ]

      },
      "Female": {
        "Gender": "Female"          
        "Country": [
                {
                  "Orientation": "Female",
                  "Name": EFGH
                },
                {
                  "Orientation": "Female",
                  "Name": IJKL        
                }
              ],
        "State": [
                {          
                  "Address": "XYZ Street",
                  "ZipCode": "US"
                }
            ]
        }
    }
  ]

}

程序:

代码语言:javascript
复制
 var Implementations = {  
  "Implementations": [
    {      
      "Male": {
        "Gender": "Male"              
      },
      "Female": {
        "Gender": "Female"       
      },

      "Country": [
        {
          "Orientation": "Male",          
          "Name": ABCD
        },
        {
          "Orientation": "Female",
          "Name": EFGH
        },
        {
          "Orientation": "Female",
          "Name": IJKL        
        }
      ],
      "State": [
        {          
          "Address": "XYZ Street",
          "ZipCode": "US"
        }
      ]
    }
  ]
}

var output = [];
for (k in Implementations.Implementations.Male) {
  var temp = [];  
  for (j in Implementations.Implementations.Male[k]) {
    temp.push({
      Country: j      
    });
  }
  output.push({
    "Implementations": k,
    Country: temp
  });
}
console.log(output);

提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-09 22:21:10

您的程序无法工作,因为Implementations.Implementations是一个数组,它没有名为Male的字段。

下面是一个有效的代码片段:

代码语言:javascript
复制
//Original JSON data in question.
var Implementations = {  
  "Implementations": [
    {      
      "Male": {
        "Gender": "Male"              
      },
      "Female": {
        "Gender": "Female"       
      },

      "Country": [
        {
          "Orientation": "Male",          
          "Name": ABCD
        },
        {
          "Orientation": "Female",
          "Name": EFGH
        },
        {
          "Orientation": "Female",
          "Name": IJKL        
        }
      ],
      "State": [
        {          
          "Address": "XYZ Street",
          "ZipCode": "US"
        }
      ]
    }
  ]
}

// Program that make the conversion
var finalResult = [];
for (var i=0; i<Implementations.Implementations.length; i++) {
  var currentImplementation = Implementations.Implementations[i];
  var targetObj = {
    "Male": {
      "Gender": "Male",
      "Country": [],
      "State": currentImplementation.State
    },
    "Female": {
      "Gender": "Female",
      "Country": [],
      "State": currentImplementation.State
    }
  };
  for (var j=0; j<currentImplementation.Country.length; j++) {
    var currentCountry = currentImplementation.Country[j];
    if (currentCountry.Orientation === 'Male') {
      targetObj.Male.Country.push(currentCountry);
    } else if (currentCountry.Orientation === 'Female') {
      targetObj.Female.Country.push(currentCountry);
    }
  }
  finalResult.push(targetObj);
}

console.log(JSON.stringify(finalResult));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56943292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档