【问题标题】:How to handle JSON Array of String in node js如何在节点 js 中处理 JSON 字符串数组
【发布时间】:2020-08-01 11:07:05
【问题描述】:

我有一个 Json,兴趣键值是字符串,如何处理它并获得我的预期输出。 包含的文件是实际数据。在这里我无法传递键值 这是我的代码

const arr = [];
const file = [
  {
    name: "same",
    hobby: '{ "name": "music", "type": "jazz" }'
  },
];
file.forEach((item) => {
  Object.keys(JSON.parse(item.hobby)).forEach((key) => {
    const base = { ...item, hobby_name: "", hobby_type: "" };
    base.hobby_name= key;
    base.hobby_type= base.hobby_type[key];
    arr.push(base);
  });
});
console.log(arr);

输出:

[ 
 {
    name: "sam",
    content: '{ "h_name": "music", "h_type": "jazz" }',
    content1: "h_name"
    constent2: ""

  },
{
    name: "sam",
    content: '{ "h_name": "music", "h_type": "jazz" }',
    content1: "h_type"
    constent2: ""

  },
];

预期输出:

 [ 
     {
        name: "sam",
        content: '{ "h_name": "music", "h_type": "jazz" }',
        content1: "h_name"
        constent2: "music"
    
      },
    {
        name: "sam",
        content: '{ "h_name": "music", "h_type": "jazz" }',
        content1: "h_type"
        constent2: "jazz"
    
      },
    ];

【问题讨论】:

    标签: node.js arrays json


    【解决方案1】:

    你需要从解析后的JSON对象中获取值:

    const arr = [];
    const file = [
      {
        name: "same",
        hobby: '{ "name": "music", "type": "jazz" }'
      },
    ];
    file.forEach((item) => {
      let hobby = JSON.parse(item.hobby);
      Object.keys(hobby).forEach((key) => {
        const base = { "name":item.name, "content":item.hobby };
        base.content1 = `h_${key}`;
        base.content2 = hobby[key];
        arr.push(base);
      });
    });
    console.log(arr);

    【讨论】:

      【解决方案2】:

      我刚刚对您的代码添加了一些更改:

      const arr = [];
      const file = [
        {
          name: "same",
          hobby: '{ "name": "music", "type": "jazz" }'
        },
      ];
      file.forEach((item) => {
        const hobbyObj = JSON.parse(item.hobby);
        
        Object.keys(hobbyObj).forEach((key) => {
          const base = { ...item, hobby_name: "", hobby_type: "" };
          
          base.hobby_name= key;
          base.hobby_type= hobbyObj[key];
          
          arr.push(base);
        });
      });
      console.log(arr);
      

      【讨论】:

        【解决方案3】:

        const file = [
          {
            name: "same",
            hobby: '{ "name": "music", "type": "jazz" }'
          },
        ];
        
        let dd = []
        
        file.forEach(
          (it)=>{
            let val = JSON.parse(it.hobby)
            Object.keys(val).map(hby=>
             {
              
              dd.push({
                name:it.name,
                content:it.hobby,
                content1:`h_${hby}`,
                constent2:val[hby]
                })
            }
          )})
        
        console.log(dd)

        【讨论】:

          猜你喜欢
          • 2021-04-11
          • 2014-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-18
          • 1970-01-01
          • 2014-03-07
          • 2013-12-22
          相关资源
          最近更新 更多