Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解析Angular Js中的非标准JSON_Javascript_Angularjs_Json - Fatal编程技术网

Javascript 解析Angular Js中的非标准JSON

Javascript 解析Angular Js中的非标准JSON,javascript,angularjs,json,Javascript,Angularjs,Json,我从一个Restful服务得到一个JSON响应,格式如下: { "comments":{ "columns":[ "clientId", "treatmentDate", "comments", "photo", "practitioner" ], "records":[ [ "1", "2016-0

我从一个Restful服务得到一个JSON响应,格式如下:

{
   "comments":{
      "columns":[
         "clientId",
         "treatmentDate",
         "comments",
         "photo",
         "practitioner"
      ],
      "records":[
         [
            "1",
            "2016-09-12",
            "Some Coments",
            "0",
            "Doc 4"
         ],
         [
            "1",
            "2016-09-11",
            "DDD oNE",
            "1",
            "Docc 3"
         ]
      ]
   }
}
记录以表名开头,后面是列和记录的单独数组。Angular不接受此格式的数据。然而,如果我提供如下标准格式的数据,它就可以完美地工作

[
   {
      "clientId":"1",
      "treatmentDate":"2016-09-12",
      "comments":"Some Coments",
      "photo":"0",
      "practitioner":"Doc 4"
   },
   {
      "clientId":"1",
      "treatmentDate":"2016-09-11",
      "comments":"DDD oNE",
      "photo":"1",
      "practitioner":"Docc 3"
   }
]

有没有一个指令可以帮我做这件事,或者我应该创建一个自定义函数,有什么想法吗?

有什么原因不能手动重新塑造数据以符合您期望的格式吗

var data = json.comments.records.map(function(record) {
    return json.comments.columns.reduce(function(reshaped, columnName, idx) {
        reshaped[columnName] = record[idx];
        return reshaped;
    }, {});
});

但要小心这一点;这要求
记录中每个数组的长度始终与列名的数量相同。

听起来您需要编写一个自定义函数来将第一个数组(列)映射到第二个数组(记录)。几个循环,你就知道json是有效的了。那么“Angular不接受数据是这种格式”是什么意思呢?谢谢,它工作得很好,因为数字始终保持不变。今天我学到了一些新东西。