0

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

如果您仔细查看原始 json 数据,我有男性/女性属性之外的国家/地区。根据国家模块内的方向属性,我希望国家模块位于男性/女性属性内。所以在后数据中,我将在男性属性中有 1 个国家模块(因为有 1 条男性记录),在女性属性中有 2 个国家模块(因为有 2 条女性记录)。

原始 json 数据如下所示:

{
  "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 数据:

{ 
  "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"
                }
            ]
        }
    }
  ]

}

程序:

 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);

先感谢您!

4

1 回答 1

1

您的程序不起作用,因为Implementations.Implementations它是一个数组,它没有名为 的字段Male

这是一个工作代码片段:

//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));
于 2019-07-09T14:21:10.040 回答