为什么我的 JSON 解析了一些缺少的键值对

Why does my JSON parse with some missing key value pairs?

本文关键字:键值对 我的 JSON 为什么      更新时间:2023-09-26

我有一个 Rails 后端,可以为这样的 JSON 提供服务:(例如是 2.json)

{"id":2,"name":"Magic","location":"Cyberjaya","surprise_type":"Great","instructions":"test","status":"awesome","pricing_level":3,"longitude":"2.90873","latitude":"101.655027","created_at":"2016-02-02T07:19:18.247Z","updated_at":"2016-02-02T12:59:14.403Z"}

以及一个 AngularJS 应用程序.js代码如下所示(仅限相关部分):

.factory('MarkersLoc', function($resource) {
  debugger;
  return $resource("http://localhost:3000/surprises/:id.json");
})
.factory('Markers', function(MarkersLoc) {
  var markers = [];
  return {
    getMarkers: function(){
        return MarkersLoc.query().$promise.then(function(response){
          debugger;
          markers = response;
        console.log("Markers: ", markers);
          return markers;
      });
    }
  }
})

我遇到的问题是,即使在调试器的位置,响应也是一个对象数组,这些对象具有 JSON 中的所有属性,除了经度、纬度、created_at和updated_at。

不幸的是,我无法确定在读取和返回时省略这四个键值对的原因。是否有一些东西应该控制我遗漏的正在处理哪个键值对?整个 JSON 不会被解析并转换为对象吗?

缺失值是后端控制器序列化数据的结果。若要查看所有键,请确保在序列化程序中定义了所有属性:

class SurpriseSerializer < ActiveModel::Serializer
  attributes :id,
             :name,
             :location,
             :surprise_type,
             :instructions,
             :status,
             :pricing_level,
             # Add the missing attributes to the serializer
             :longitude,
             :latitude,
             :created_at,
             :updated_at
end