重组json缺少的元素

时间:2019-07-22 21:27:26

标签: javascript node.js json node-modules

原始json数据:

{
  "UniversalOne": "",
  "CommonOne": ""
  "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"
        }
      ]
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {      
      "Type": "Negative" 
    }  
  ],
  "UniversalTwo": "",  
  "CommonTwo": "",  
  "EatingHabits": {    
    "Type": "Excessive"
  },
  "ReadingHabits": {    
    "Type": "Fast"    
  },
  "FitnessHabits": {   
  },
  "UniversalThree": "",
  "CommonThree": ""
}

预期的json数据:

{ 
  "UniversalOne": "",
  "CommonOne": ""
  "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"
                }
            ]
        }
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {      
      "Type": "Negative" 
    }  
  ],
  "UniversalTwo": "",  
  "CommonTwo": "",  
  "EatingHabits": {    
    "Type": "Excessive"
  },
  "ReadingHabits": {    
    "Type": "Fast"    
  },
  "FitnessHabits": {   
  },
  "UniversalThree": "",
  "CommonThree": ""

}

程序:

//Original JSON data in question.
var Implementations = {
  "UniversalOne": "",
  "CommonOne": ""
  "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"
        }
      ]
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {      
      "Type": "Negative" 
    }  
  ],
  "UniversalTwo": "",  
  "CommonTwo": "",  
  "EatingHabits": {    
    "Type": "Excessive"
  },
  "ReadingHabits": {    
    "Type": "Fast"    
  },
  "FitnessHabits": {   
  },
  "UniversalThree": "",
  "CommonThree": ""
}

// 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 个答案:

答案 0 :(得分:1)

如果我正确地回答了您的问题,我认为您的代码已经为您提供了预期的JSON for 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"
        }
      ]
    }
  }
]

因此,如果您要询问如何添加其余属性以实现期望的JSON,则可以执行以下操作:

Implementations.Implementations = finalResult;

将原来的JSON实现属性替换为您创建的属性。

因此说:

var Implementations = {
  "UniversalOne": "",
  "CommonOne": ""
  "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"
        }
      ]
    }
  ],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {      
      "Type": "Negative" 
    }  
  ],
  "UniversalTwo": "",  
  "CommonTwo": "",  
  "EatingHabits": {    
    "Type": "Excessive"
  },
  "ReadingHabits": {    
    "Type": "Fast"    
  },
  "FitnessHabits": {   
  },
  "UniversalThree": "",
  "CommonThree": ""
}

如果您执行Implementations.Implementations = filteredResult; 实现将变为:

{
  "UniversalOne": "",
  "CommonOne": ""
  "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"
        }
      ]
    }
  }
],
  "PersonalityTraits": [
    {
      "Type": "Positive"
    },
    {      
      "Type": "Negative" 
    }  
  ],
  "UniversalTwo": "",  
  "CommonTwo": "",  
  "EatingHabits": {    
    "Type": "Excessive"
  },
  "ReadingHabits": {    
    "Type": "Fast"    
  },
  "FitnessHabits": {   
  },
  "UniversalThree": "",
  "CommonThree": ""
}

否则,请说明您要实现的目标。

相关问题