在AngularJS中解析嵌套对象的最佳方法?

时间:2015-07-08 15:21:45

标签: json angularjs object nested

我正在尝试使用angularjs来解析从远程服务器返回的嵌套数据结构。我真的被这种模式困扰,因为我试图通过以下功能访问“事件”数据

$scope.generate_event = function(){
            from_date = $scope.dts.from
            to_date = $scope.dts.to

            from = from_date.getFullYear()+'/'+(from_date.getMonth()+1)+'/'+from_date.getDate()
            to = to_date.getFullYear()+'/'+(to_date.getMonth()+1)+'/'+to_date.getDate()
            $http.get(server+'rawdata?vids='+$scope.selected_vehicle.id+'&evfields=lat,lon,f_event_time,speed&from='+from+'&to='+to)
            .success(function(data){
                $scope.report_data = data
                $localStorage.report_data = data
                $scope.generate()
            })
        }

任何建议,甚至是对最佳方法的暗示都会很棒,我需要这项工作并且现在需要一个月。谢谢!

{
"rawData": {
    "keys": {
        "lat": ["number", "lat"],
        "lon": ["number", "lon"],
        "speed": ["number", "Speed [mph]"],
        "code": ["number", "EVC"],
        "vid": ["number", "Vehicle ID"]
    },
    "keys_order": ["lat", "lon", "speed", "code", "vid"],
    "events": [{
        "f_lon": -8.3315599999999996,
        "code": 4,
        "vid": 5,
        "lon": -833156,
        "f_lat": 51.90831,
        "lat": 5190831,
        "speed": 78.0
    }, {
        "f_lon": -8.3741599999999998,
        "code": 4,
        "vid": 5,
        "lon": -837416,
        "f_lat": 51.903979999999997,
        "lat": 5190398,
        "speed": 78.0
    }]
}

}

更新:我没有正确解释问题。这是生成函数

$scope.generate = function(){
            $scope.event_config = {
                title: 'Events',    // chart title, legend etc
            /*etc
             *etc
             */
            data = {}
            data.series = [' Events']
            data.data = []
 this fucker ----->$scope.report_data.events.forEach(function(value, index, array){
                o = {}
                o.x = value.f_event_time
                o.y = [value.lat+'/'+value.lon]
                o.tooltip = value.speed
                data.data.push(o)
            })
            $scope.event_data = data

我收到错误'forEach undefined'。这应该生成d3图表,但是'report_data'最初用于存储来自不同功能的距离数据以用于本地存储。那么我需要localStorage的第二个变量吗?即$ scope.report_event = $ localStorage.report_event?如果我发送它,有人可以查看源代码吗?

2 个答案:

答案 0 :(得分:0)

$http.get(server+'rawdata?vids='+$scope.selected_vehicle.id+'&evfields=lat,lon,f_event_time,speed&from='+from+'&to='+to)
            .success(function(data){
                if (data) {
                   var events = data.rawData.events; // get the events json array
                   $scope.generate(events);
                }
            })

并在您的控制器中具有声明性功能,如

$scope.generate = function(events) {
    // your code
}

答案 1 :(得分:0)

使用以下

解决了这个问题
$scope.generate_report = function(){
            from_date = $scope.dts.from
            to_date = $scope.dts.to

            from = from_date.getFullYear()+'/'+(from_date.getMonth()+1)+'/'+from_date.getDate()
            to = to_date.getFullYear()+'/'+(to_date.getMonth()+1)+'/'+to_date.getDate()
            $http.get(server+'vehicle/'+$scope.selected_vehicle.id+'/counters/deltas/day?from='+from+'&to='+to)
            .success(function(data){
                $scope.report_data = data
                $localStorage.report_data = data
                $scope.process()
            })
            $http.get(server+'rawdata?vids='+$scope.selected_vehicle.id+'&genevcodes=39,40&evfields=lat,lon,f_event_time,mph,speed,code&from='+from+'&to='+to)
            .success(function(data){
                if(data){
                    var events = data.rawData.events
                }
                $scope.report_event = events
                $localStorage.report_event = events
                $scope.generate()
            })
        }

只需要声明另一个localstorage变量来存储来自JSON对象的事件谢谢Yannik