如何从现有的json创建新的json

时间:2017-01-23 17:59:27

标签: angularjs json

//我现有的Json

    [
    {
          "Id":1,
          "Authors":[
             {
                "Id":10,
                "Name":"Arun"
             },
             {
                "Id":14,
                "Name":"Arjun"
             }
          ],
          "tags":[
             {
                "Name":"Java"
             }
          ]
    },
    {
          "Id":5,
          "Authors":[
             {
                "Id":7,
                "Name":"Shyama"
             }
          ],
          "tags":[
             {
                "Name":"C#"
             },
             {
                "Name":"C++"
             }
          ]
       }
    ]

//新Json(已转换) //我想在这里结合作者

    [
    {
          "Id":1,
          "Authors":"Arun, Arjun",
          "tags":"Java"
    },
    {
          "Id":5,
          "Authors":"Shyama",
          "tags":"C#,C++"
    }
    ]

//我创建的控制器

$ scope.request = response.data; //请求包含所有json数据

// ----------创建新json的逻辑------------     }

1 个答案:

答案 0 :(得分:0)

var json = [
    {
          "Id":1,
          "Authors":[
             {
                "Id":10,
                "Name":"Arun"
             },
             {
                "Id":14,
                "Name":"Arjun"
             }
          ],
          "tags":[
             {
                "Name":"Java"
             }
          ]
    },
    {
          "Id":5,
          "Authors":[
             {
                "Id":7,
                "Name":"Shyama"
             }
          ],
          "tags":[
             {
                "Name":"C#"
             },
             {
                "Name":"C++"
             }
          ]
       }
    ];
for (var i=0, j=json.length; i<j; i++) {
    json[i].Authors = json[i].Authors.map(function(item) {return item.Name;}).join(',');
    json[i].tags = json[i].tags.map(function(item) {return item.Name;}).join(',');
}
console.log(json);