【问题标题】:restructure json missing elements重构json缺失元素
【发布时间】:2019-11-30 21:34:35
【问题描述】:

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

如何在当前程序的 Implementations 对象之外添加诸如 Personality Traits、Eating Habits、Reading Habits、Fitness Habits 等对象以及 Universal 和 common 等属性?

【问题讨论】:

  • 向 JavaScript 对象添加属性是一件基本的事情。到目前为止,您尝试过什么?
  • for (var k=0; j

标签: javascript node.js json node-modules


【解决方案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": ""
}

否则,请多解释一下您想要实现的目标。

【讨论】:

  • 该程序仅处理实现属性。如果您在我的预期响应中向下滚动,我如何在实施后添加人格特征、阅读习惯、饮食习惯、健身习惯?为了简短起见,我希望通过调整从有问题的预期 json 响应中获得准确的响应当前程序。目前,当我运行程序时,我看不到任何在人格特征到底部之后的任何内容。
  • originalJSON 是一个模块吗?我收到未定义错误。
  • 能否在评论中添加sn-p?我完全不明白如何实施您的回复。我是否将整个 Json 添加到另一个变量中?
  • @Eva 我将制作一个更小的对象,因为我无法复制和粘贴您的 JSON 对象。 var x = {UniversalOne:“”,CommonOne:“”,实现:[]};如果你这样做 x.Implementations = filteredResult;根据原始 x,它仍然具有 UniversalOne、CommonOne,但 x.Implementations 将替换为过滤结果。希望这是有道理的
  • 知道了,现在说得通了——非常感谢!尝试上述方法后,我遇到了 for (var i=0; i
猜你喜欢
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
  • 2023-02-11
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
相关资源
最近更新 更多