如何解析关于时间戳的JSON

时间:2015-04-01 06:16:33

标签: arrays json angularjs

这是我的控制器

  var app = angular.module("app", []);

    app.controller('Ctrl',["$scope","$http", function($scope,$http,) {

        $scope.Observations=[];
        $scope.totalOutputData =[];
        $scope.tiltedIrradianceData =[];
        $scope.time =[];
        $scope.demoJsonList = {
        "took": 1,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
        },
        "hits": {
            "total": 2,
            "max_score": 1,
            "hits": [
                {
                    "_index": "solarpowerdemo1",
                    "_type": "solar1",
                    "_id": "1",
                    "_score": 1,
                    "_source": {
                        "observations": [
                            {
                                "sensor": "weather",
                                "record": [
                                    {
                                        "output": [
                                            {
                                                "name": "tilted_irradiance",
                                                "value": 8,
                                                "type": "Double",
                                                "unit": "W/m2"
                                            },
                                            {
                                                "name": "horizontal_irradiance",
                                                "value": 23,
                                                "type": "Double",
                                                "unit": "W/m2"
                                            },
                                            {
                                                "name": "ambient_air_temperature",
                                                "value": 20,
                                                "type": "Double",
                                                "unit": "°C"
                                            },
                                            {
                                                "name": "relative_humidity",
                                                "value": 35,
                                                "type": "Double",
                                                "unit": "%"
                                            },
                                            {
                                                "name": "wind_direction",
                                                "value": 27,
                                                "type": "Double",
                                                "unit": "degrees"
                                            },
                                            {
                                                "name": "wind_speed",
                                                "value": 27,
                                                "type": "Double",
                                                "unit": "m/s"
                                            },
                                            {
                                                "name": "Precipitation",
                                                "value": 27,
                                                "type": "Double",
                                                "unit": "mm/h"
                                            },
                                            {
                                                "name": "module_temperature",
                                                "value": 27,
                                                "type": "Double",
                                                "unit": "°C"
                                            }
                                        ],
                                        "starttime": "2014-07-19T10:37:19.000",
                                        "position_global": {
                                            "altitude": "10m",
                                            "location": {
                                                "lat": 22.610927503182157,
                                                "lon": 88.45465055391536
                                            }
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                },
                {
                    "_index": "solarpowerdemo1",
                    "_type": "solar1",
                    "_id": "2",
                    "_score": 1,
                    "_source": {
                        "observations": [
                            {
                                "sensor": "weather",
                                "record": [
                                    {
                                        "output": [
                                            {
                                                "name": "tilted_irradiance",
                                                "value": 9.32,
                                                "type": "Double",
                                                "unit": "W/m2"
                                            },
                                            {
                                                "name": "horizontal_irradiance",
                                                "value": 23.23,
                                                "type": "Double",
                                                "unit": "W/m2"
                                            },
                                            {
                                                "name": "ambient_air_temperature",
                                                "value": 20.66,
                                                "type": "Double",
                                                "unit": "°C"
                                            },
                                            {
                                                "name": "relative_humidity",
                                                "value": 68,
                                                "type": "Double",
                                                "unit": "%"
                                            },
                                            {
                                                "name": "wind_direction",
                                                "value": 27.88,
                                                "type": "Double",
                                                "unit": "degrees"
                                            },
                                            {
                                                "name": "wind_speed",
                                                "value": 27.22,
                                                "type": "Double",
                                                "unit": "m/s"
                                            },
                                            {
                                                "name": "Precipitation",
                                                "value": 27.22,
                                                "type": "Double",
                                                "unit": "mm/h"
                                            },
                                            {
                                                "name": "module_temperature",
                                                "value": 32,
                                                "type": "Double",
                                                "unit": "°C"
                                            }
                                        ],
                                        "starttime": "2015-07-19T11:38:19.000",
                                        "position_global": {
                                            "altitude": "10m",
                                            "location": {
                                                "lat": 22.6109257,
                                                "lon": 88.4546501536
                                            }
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                },  {
                    "_index": "solarpowerdemo1",
                    "_type": "solar1",
                    "_id": "2",
                    "_score": 1,
                    "_source": {
                        "observations": [
                            {
                                "sensor": "weather",
                                "record": [
                                    {
                                        "output": [
                                            {
                                                "name": "tilted_irradiance",
                                                "value": 19.32,
                                                "type": "Double",
                                                "unit": "W/m2"
                                            },
                                            {
                                                "name": "horizontal_irradiance",
                                                "value": 123.23,
                                                "type": "Double",
                                                "unit": "W/m2"
                                            },
                                            {
                                                "name": "ambient_air_temperature",
                                                "value": 210.66,
                                                "type": "Double",
                                                "unit": "°C"
                                            },
                                            {
                                                "name": "relative_humidity",
                                                "value": 168,
                                                "type": "Double",
                                                "unit": "%"
                                            },
                                            {
                                                "name": "wind_direction",
                                                "value": 217.88,
                                                "type": "Double",
                                                "unit": "degrees"
                                            },
                                            {
                                                "name": "wind_speed",
                                                "value": 227.22,
                                                "type": "Double",
                                                "unit": "m/s"
                                            },
                                            {
                                                "name": "Precipitation",
                                                "value": 207.22,
                                                "type": "Double",
                                                "unit": "mm/h"
                                            },
                                            {
                                                "name": "module_temperature",
                                                "value": 312,
                                                "type": "Double",
                                                "unit": "°C"
                                            }
                                        ],
                                        "starttime": "2015-08-19T11:38:19.000",
                                        "position_global": {
                                            "altitude": "10m",
                                            "location": {
                                                "lat": 22.6109257,
                                                "lon": 88.4546501536
                                            }
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    };

        $scope.obsList = function() {


            $scope.Observations = $scope.demoJsonList.hits.hits;
            console.log($scope.Observations.length);

            for(var j=0;j<$scope.Observations.length;j++){
            console.log($scope.Observations[j]);


            console.log($scope.Observations[j]._source.observations);

            $scope.records = [];

            $scope.globalPosition =[];
            $scope.obs1=[];
            $scope.AllOutputs =[];

            angular.forEach($scope.Observations[j]._source.observations, function(obs, index) {
                angular.forEach(obs.record, function(recrd, index){
                    $scope.records.push(recrd);
                });
            });
            console.log(angular.toJson($scope.records));
            console.log($scope.records.length);
            $scope.AllOutputs = angular.copy($scope.records[0].output);

            $scope.time.push($scope.records[0].starttime);

            console.log($scope.AllOutputs.length);
            angular.forEach($scope.AllOutputs, function(ao, index){
                    $scope.totalOutputData.push(ao);


                })
                console.log($scope.totalOutputData);


            }
            console.log($scope.AllOutputs);

            for(var m=0;m<$scope.time.length;m++){
                var datObj={
                    starttime:$scope.time[m],
                    name:[],
                    value:[]
                }

            }   
                $scope.value =[];
                $scope.name =[];
                angular.forEach($scope.totalOutputData, function(tod, index){
                    $scope.name.push(tod.name);
                    $scope.value.push(tod.value);

                });
                console.log($scope.value);

            };
        $scope.obsList();       

    }]);

我希望输出如下:

Object {starttime: "2014-07-19T10:37:19.000", name: Array[24], value: Array[24]}
Object {starttime: "2015-07-19T11:38:19.000", name: Array[24], value: Array[24]}
Object {starttime: "2015-08-19T11:38:19.000", name: Array[24], value: Array[24]}

但是这里所有的值都是在名称和值中插入的,我希望在第一个时间戳中插入前8个值,然后为下一个时间戳插入下一个8个值,依此类推。

3 个答案:

答案 0 :(得分:0)

如果你想使用undersore.js,你可以使用这个链,或用自定义代码替换这些函数:

_(_(_.flatten(_.flatten(list.hits.hits.map(function(d) {
  return d._source.observations;
})).map(function(d) {
  return d.record;
})))).map(function(d) {
  return {
    starttime: d.starttime,
    names: _(d.output).map(function(n) {
      return n.name;
    }),
    values: _(d.output).map(function(o) {
      return o.value;
    })
  }
});

说明:首先,您将列表映射并展平为重要部分(点击,然后记录)。现在,您可以将该记录列表转换为所需的输出格式(startime,名称,值)。

答案 1 :(得分:0)

用下面的代码替换你的控制器方法,这将产生你上面所期望的输出。

$scope.obsList = function() {


    $scope.Observations = $scope.demoJsonList.hits.hits;
    var outputJson = [];

    for (var j = 0; j < $scope.Observations.length; j++) {

        var record = $scope.Observations[j]._source.observations[0].record[0];
        var starttime1 = record.starttime;

        var outputRecords = record.output;
        var name1 = [];
        var value1 = [];

        for (var i = 0; i < outputRecords.length; i++) {
            name1.push(outputRecords[i].name);
            value1.push(outputRecords[i].value);
        }

        var rec = {
            starttime: starttime1,
            name: name1,
            value: value1
        };
        outputJson.push(rec);

        console.log(outputJson);
    }

};

答案 2 :(得分:0)

可以使用

里面的(var m = 0; m

for(var k =($ scope.AllOutputs.length)m; k($ scope.AllOutputs.length)); k ++){

            datObj.name.push($scope.totalOutputData[k].name);
            datObj.value.push($scope.totalOutputData[k].value);
        }