Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Json 将响应从后端序列化到存储余烬存储_Json_Ember.js_Ember Data_Ember Cli - Fatal编程技术网

Json 将响应从后端序列化到存储余烬存储

Json 将响应从后端序列化到存储余烬存储,json,ember.js,ember-data,ember-cli,Json,Ember.js,Ember Data,Ember Cli,我从后端的响应不是以哪个余烬商店的形式。我无法序列化响应 response.json [{ "pk": 127, "url": "http://example.com/api/galleries/127/", "gallery_name": "Faces", "thumbnail_url": "https://example.cloud.net/galleryThumbs/2656a05c-4ec7-3eea-8c5e-d8019454d443.jpg",

我从后端的响应不是以哪个余烬商店的形式。我无法序列化响应

response.json

[{
    "pk": 127,
    "url": "http://example.com/api/galleries/127/",
    "gallery_name": "Faces",
    "thumbnail_url": "https://example.cloud.net/galleryThumbs/2656a05c-4ec7-3eea-8c5e-d8019454d443.jpg",
    "time": "1 month ago",
    "description": "Created by user",
    "is_following": true,
    "feedPhotos": [{
            "pk": 624,
            "url": "http://example.com/api/photos/624/",
            "profilePic": "https://example.cloud.net/userDPs/50906ce2-394d-39c8-9261-8cf78e3611c2.jpg",
            "userName": "Nabeela",
            "userKarma": 915,
            "caption": "Old woman spinning her 'chhos-khor' ...a rotation of which is equivalent to the recitation of a mantra.",
            "numComments": 0,
            "owner": "http://example.com/api/users/44/",
            "time": "1 month ago",
            "photo_url": "https://example.cloud.net/photos/9cbd6423-3bc5-36e0-b8b4-d725efb3249a.jpg",
            "comments_url": "http://example.com/api/photos/624/comments/",
            "numFives": 4,
            "fivers_url": "http://example.com/api/photogalleries/1362/fivers/",
            "fivers_pk": 1362,
            "fullphoto_url": "http://example.com/api/photogalleries/1362/photo/",
            "fullphoto_pk": 1362,
            "is_fived": true,
            "hiFiveKarma": 1,
            "owner_pk": 44,
            "userFirstName": "Nabeela",
            "is_bookmarked": false
        }, {
            "pk": 574,
            "url": "http://example.com/api/photos/574/",
            "profilePic": "https://example.cloud.net/userDPs/b6f69e4e-980d-3cc3-8b3e-3eb1a7f21350.jpg",
            "userName": "Rohini",
            "userKarma": 194,
            "caption": "Life @ Myanmar!",
            "numComments": 0,
            "owner": "http://example.com/api/users/45/",
            "time": "2 months ago",
            "photo_url": "https://example.cloud.net/photos/eeae72d5-d6af-391e-a218-b442c0c7e34e.jpg",
            "comments_url": "http://example.com/api/photos/574/comments/",
            "numFives": 2,
            "fivers_url": "http://example.com/api/photogalleries/1303/fivers/",
            "fivers_pk": 1303,
            "fullphoto_url": "http://example.com/api/photogalleries/1303/photo/",
            "fullphoto_pk": 1303,
            "is_fived": false,
            "hiFiveKarma": 0,
            "owner_pk": 45,
            "userFirstName": "Rohini",
            "is_bookmarked": false
        }
    ]
}, {
    "pk": 65,
    "url": "http://example.com/api/galleries/65/",
    "gallery_name": "Royal",
    "thumbnail_url": "https://example.cloud.net/galleryThumbs/d8a900af-1f1d-3977-8cc8-b8bb36e32be5.jpg",
    "time": "2 months ago",
    "description": "This is a gallery about Royal",
    "is_following": false,
    "feedPhotos": [{
        "pk": 347,
        "url": "http://example.com/api/photos/347/",
        "profilePic": "https://example.cloud.net/userDPs/50906ce2-394d-39c8-9261-8cf78e3611c2.jpg",
        "userName": "Nabeela",
        "userKarma": 915,
        "caption": "I cannot forget the name of this palace - Moti Mahal (translation: Pearl Palace). Indescribably beautiful, ainnit! at Mehrangarh fort, Jodhp",
        "numComments": 0,
        "owner": "http://example.com/api/users/44/",
        "time": "2 months ago",
        "photo_url": "https://example.cloud.net/photos/958ed406-708e-3f01-a2f4-9467cd709fdd.jpg",
        "comments_url": "http://example.com/api/photos/347/comments/",
        "numFives": 4,
        "fivers_url": "http://example.com/api/photogalleries/759/fivers/",
        "fivers_pk": 759,
        "fullphoto_url": "http://example.com/api/photogalleries/759/photo/",
        "fullphoto_pk": 759,
        "is_fived": false,
        "hiFiveKarma": 0,
        "owner_pk": 44,
        "userFirstName": "Nabeela",
        "is_bookmarked": false
    }, {
        "pk": 593,
        "url": "http://example.com/api/photos/593/",
        "profilePic": "https://example.cloud.net/userDPs/95ac6974-f7df-338c-ab84-99fa1df7514c.jpg",
        "userName": "Vikanshu",
        "userKarma": 932,
        "caption": "Marvelous architecture!! in Florence, Italy",
        "numComments": 0,
        "owner": "http://example.com/api/users/48/",
        "time": "1 month ago",
        "photo_url": "https://example.cloud.net/photos/7a86eb37-6c68-3d6c-b6cf-2e3b74d330dd.jpg",
        "comments_url": "http://example.com/api/photos/593/comments/",
        "numFives": 4,
        "fivers_url": "http://example.com/api/photogalleries/1363/fivers/",
        "fivers_pk": 1363,
        "fullphoto_url": "http://example.com/api/photogalleries/1363/photo/",
        "fullphoto_pk": 1363,
        "is_fived": false,
        "hiFiveKarma": 0,
        "owner_pk": 48,
        "userFirstName": "Vikanshu",
        "is_bookmarked": false
    }]
}]
如何使用JsonPiserAlizer或ember cli中的任何其他序列化程序对其进行序列化,以便将其存储在ember store中

++++顶级: 根目录: JSON对象必须是每个JSON API请求响应的根。 文档必须至少包含一个顶级成员: 1.数据:文档“主要数据” 2.错误:错误对象数组(id、状态、代码、标题……) 3.meta:包含非标准元信息(版权、作者…)的元对象

成员数据和错误不能同时存在。 “数据”{}

+++++资源对象 1.资源对象必须至少包含以下顶级成员 *身份证 *类型

  • 此外,资源对象可以包含这些顶级成员中的任何一个 *属性 *关系 *链接 *元
  • //首先添加属性

    ```
    //structure-2
    //for galleries
    {
      "data": {
        "type": "galleries",
        "id": "1",
        "attributes": {
          "galleryName": "Faces"
          "thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
          "description": "Created by user",
        }
      }
    }
    
    //for photos
    {
      "data": {
        "type": "photos",
        "id": "1",
        "attributes":{
           userName: "Nabeela",
           userKarma: 915
         }
      }
    }
    ```
    
    //添加关系 关系对象必须至少包含以下内容之一 *链接(至少包含一个“自我”或“相关”资源链接 *资料 *元

    //关系中的数据

    ```
    //structure-4
    //for galleries
    {
      "data": [{
        "type": "galleries",
        "id": "1",
        "attributes": {
          "galleryName": "Faces"
          "thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
          "description": "Created by user",
        },
        "relationships":{ //This has all your photo stuff
           "links":{
          "self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
        },
        "data": {       //picked it up from structure-1
              "type": "photos",
              "id": "77"
        }
        }]
      }
    }
    ```
    
    //添加相关资源“包括”

    ``` //画廊

    {
      "data": [{
        "type": "galleries",
        "id": "1",
        "attributes": {
          "galleryName": "Faces"
          "thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
          "description": "Created by user",
        },
        "relationships":{ //This has all your photo stuff
           "links":{
          "self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
        },
        "data": {       //picked it up from structure-1
              "type": "photos",
              "id": "77"
        }
        }],
        "included":[{
            "type": "photos",
            "id": "77",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            },
        {
            "type": "photos",
            "id": "78",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            }
       }]
    
      }
    }
    
    {
      "data": [{
        "type": "galleries",
        "id": "1",
        "attributes": {
          "galleryName": "Faces"
          "thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
          "description": "Created by user",
        },
        "relationships":{ //This has all your photo stuff
           "links":{
          "self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
        },
        "data": {       //picked it up from structure-1
              "type": "photos",
              "id": "77"
        }
        },{
        "type": "galleries",
        "id": "2",
        "attributes": {
          "galleryName": "Faces"
          "thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
          "description": "Created by user",
        },
        "relationships":{ //This has all your photo stuff
           "links":{
          "self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
        },
        "data": {       //picked it up from structure-1
              "type": "photos",
              "id": "79"
        }
        }],
        "included":[{
            "type": "photos",
            "id": "77",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            },{
            "type": "photos",
            "id": "78",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            },{
            "type": "photos",
            "id": "79",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            }
       }]
    
      }
    }
    
    ``` 收集。我没有信心,但试试这个

    现在是画廊收藏

    //画廊

    {
      "data": [{
        "type": "galleries",
        "id": "1",
        "attributes": {
          "galleryName": "Faces"
          "thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
          "description": "Created by user",
        },
        "relationships":{ //This has all your photo stuff
           "links":{
          "self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
        },
        "data": {       //picked it up from structure-1
              "type": "photos",
              "id": "77"
        }
        }],
        "included":[{
            "type": "photos",
            "id": "77",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            },
        {
            "type": "photos",
            "id": "78",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            }
       }]
    
      }
    }
    
    {
      "data": [{
        "type": "galleries",
        "id": "1",
        "attributes": {
          "galleryName": "Faces"
          "thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
          "description": "Created by user",
        },
        "relationships":{ //This has all your photo stuff
           "links":{
          "self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
        },
        "data": {       //picked it up from structure-1
              "type": "photos",
              "id": "77"
        }
        },{
        "type": "galleries",
        "id": "2",
        "attributes": {
          "galleryName": "Faces"
          "thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
          "description": "Created by user",
        },
        "relationships":{ //This has all your photo stuff
           "links":{
          "self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
        },
        "data": {       //picked it up from structure-1
              "type": "photos",
              "id": "79"
        }
        }],
        "included":[{
            "type": "photos",
            "id": "77",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            },{
            "type": "photos",
            "id": "78",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            },{
            "type": "photos",
            "id": "79",
            "attributes":{
                userName: "Nabeela",
                userKarma: 915
            }
       }]
    
      }
    }
    
    =============实施部分=================================

    JSONSerializer规范化过程遵循以下步骤

    *normalizeResponse:输入方法。 *normalizeCreateRecordResponse:特定操作的normalizeResponse。 *normalizeSingleResponse | normalizeArrayResponse: -对于像createRecord这样的方法,我们希望返回一条记录。 -对于findAll这样的方法,我们希望返回多个记录

    +正常化= normalizeArray迭代并调用每个记录的normalize normalizeSingle调用它一次

    +extractID | extractAttributes | extractRelationships =规范化这些方法的委托,以将记录负载转换为jsonAPI格式

    从normalizeResponse方法开始。如果打开并看到normalizeResponse方法

    在json序列化程序中 链接正常化响应:

    data/lib/serializers/json serializer.js#L192

    您可以找到一个开关盒
    开关(requestType)
    然后调用“normalizeFindRecordResponse” “queryRecord”然后调用“normalizeQueryRecordResponse” “findAll”,然后调用“normalizeFindAllResponse” ……等等。 如果您注意到传递给所有方法的参数与normalize的参数相同

    (…论点):)

    **让我们从芬德尔开始 i、 e normalizeResponse->normalizeFindAllResponse->normalizeArrayResponse 正如normalizeFindAllResponse方法只有一行调用

    正常化反应

    normalizeResponse->normalizeFindAllResponse->normalizeArrayResponse->

    _normalizeResponse{extractMeta,normalize}

    extractMeta[提取分页等元信息] 如果是单个:正常化[]

    emberjs文档中的规范化方法示例

    ```
    import DS from 'ember-data';
    
    export default DS.JSONSerializer.extend({
      normalize: function(typeClass, hash) {
        var fields = Ember.get(typeClass, 'fields');
        fields.forEach(function(field) {
          var payloadField = Ember.String.underscore(field);
          if (field === payloadField) { return; }
    
          hash[field] = hash[payloadField];
          delete hash[payloadField];
        });
        return this._super.apply(this, arguments);
      }
    });
    ```
    
    “normalizerRayResponse调用`返回此。\ u normalizerResponse

    (存储,primaryModelClass,有效负载,id,requestType,false)。 所以isSingle对于_normalizeResponse方法来说是错误的。所以我们必须推所有的

    包含数组的相关记录 在我们的例子中,下面的照片是从“_normalizeRespose”中提取的片段

    方法。

    在服务器的JSON响应上下文中,事情仍然不清楚 但至少我们现在知道了流程

    让我们尝试将其应用于findAll(按照上面的流程)

  • 运行“ember g序列化程序应用程序”//假设您正在使用ember cli,并且
  • 打算使此序列化程序成为应用程序的通用序列化程序

    到目前为止,我没有关于如何以及何时调用normalizeResponse的信息。:( 我只是浏览了一下,猜测一下从商店呼叫的服务器接收到的数据

    normalizeResponseHelpers,它依次调用normalizeResponse

    在任何情况下,“normalizeResponse”都将发送有效负载和其他必要的数据

    用于规范化FindallResponse(…参数)的信息,这些参数将调用

    normalizeArrayResponse(…参数),它将依次调用“\u normalizeResponse”

    这就是我们需要采取行动的地方 对于extraMeta和normalize。 +外元 我不确定您的json响应中是否有任何元信息。 如果有,您可以参考文档中提到的示例

    所以我想您可以直接在应用程序中使用示例中的normalize方法;)

    请尝试检查。由于我自己正在学习ember,我不能保证它会起作用,但它应该起作用。lonngggg解释是我在学习问题/解决方案时的想法

    //app/serializers/application.js
    
    +normalize
    ```
    import DS from 'ember-data';
    
    export default DS.JSONSerializer.extend({
      normalize: function(typeClass, hash) {
        var fields = Ember.get(typeClass, 'fields');
        fields.forEach(function(field) {
          var payloadField = Ember.String.underscore(field);
          if (field === payloadField) { return; }
    
          hash[field] = hash[payloadField];
          delete hash[payloadField];
        });
        return this._super.apply(this, arguments);
      }
    });
    ```
    
    来自服务器的JSON中的主键是pk。您也必须提到这一点

    app/serializers/application.js

    import DS from 'ember-data';
    
    export default DS.JSONSerializer.extend({
      primaryKey: 'pk'
    });
    

    它可以工作,但只存储RESTNSE的第一个结果。我的模型是这样的。gallery\u name:attr('string')、缩略图\u url:attr('string')、下面是:attr('boolean')、time:attr('string')、description:attr('string')、feedPhotos:attr(),我的路线是this.store.findAll('feed')),我在feedPhotos中看到了pk,我假设gallery有很多feedPhotos。因此feedPhotos应该是feedPhotos:DS.hasMany('photos')//类似的内容。我已经更新了primaryKey的答案,请选中please mark accepted,如果答案有效。您可以使用来重现您的问题,以便更多的人能够提供帮助
    import DS from 'ember-data';
    
    export default DS.JSONSerializer.extend({
      primaryKey: 'pk'
    });