Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
angular 2中对象的API Json_Json_Api_Angular_Object_Typescript - Fatal编程技术网

angular 2中对象的API Json

angular 2中对象的API Json,json,api,angular,object,typescript,Json,Api,Angular,Object,Typescript,我有一个Json API,其结构如下 `{ "type":"champion", "version":"7.8.1", "data": { "Jax": { "id":24, "key":"Jax", "name":"Jax", "title":"o Grão-Mestre das Armas", "image":

我有一个Json API,其结构如下

`{
    "type":"champion",
    "version":"7.8.1",
    "data":
     {
       "Jax":
       { 
            "id":24,
            "key":"Jax",
            "name":"Jax",
            "title":"o Grão-Mestre das Armas",
            "image":
            {
                  "full":"Jax.png",
                  "sprite":"champion1.png",
                  "group":"champion",
                  "x":144,
                  "y":48,
                  "w":48,
                  "h":48
              }
         }
......`

And I need to tranform this in a Object
My classes for this are this:

`export class Champion{
    private type: string;
    private version: string;
    private data: MasterModel[];
    from(data: any): Champion {
        this.type = data.type;
        this.version = data.version;
        this.data = data.data;
        return this;
    }
}`

这是正确的吗?将这些数据放入对象的最佳方式是什么? 我在做什么

--服务

但这不起作用 idk如果有帮助,但我想这样做->


有人帮我吗?Thankkks

我建议您实际使用接口。因为您知道数据的模型,所以只需将它们键入接口即可。在本例中,我将JSON重新构造为以下格式:

{
  type: "champion",
  version: "7.8.1",
  champions: [
    {
      // each champion details here
    }
  ]
}
其中
champion
是一个对象,它包含所有champion的数组。如果您想以不同的方式构建它,可以对代码进行适当的更改

服务代码简单地映射如下:

.map((res:Response) => res.json())
真正的魔法发生在组件中:p

  .subscribe(data => {
    // add the type and version and empty array of champions        
    this.champion = {type:data.type,version:data.version,champions:[]}      
    let keyArr: any[] = Object.keys(data.data),
    keyArr.forEach((key: any) => {
        // push values of each champion to the array
        this.champion.champions.push(data.data[key]);
    });
  })
现在你有了一个漂亮的小物件,上面有一排冠军,你可以在上面施展你的魔法:)

这里有一个

我还没有完全制作接口,但我建议您这样做:)


希望这对您有所帮助,或者至少能将您推向正确的方向:)

我建议您实际使用接口。因为您知道数据的模型,所以只需将它们键入接口即可。在本例中,我将JSON重新构造为以下格式:

{
  type: "champion",
  version: "7.8.1",
  champions: [
    {
      // each champion details here
    }
  ]
}
其中
champion
是一个对象,它包含所有champion的数组。如果您想以不同的方式构建它,可以对代码进行适当的更改

服务代码简单地映射如下:

.map((res:Response) => res.json())
真正的魔法发生在组件中:p

  .subscribe(data => {
    // add the type and version and empty array of champions        
    this.champion = {type:data.type,version:data.version,champions:[]}      
    let keyArr: any[] = Object.keys(data.data),
    keyArr.forEach((key: any) => {
        // push values of each champion to the array
        this.champion.champions.push(data.data[key]);
    });
  })
现在你有了一个漂亮的小物件,上面有一排冠军,你可以在上面施展你的魔法:)

这里有一个

我还没有完全制作接口,但我建议您这样做:)


希望这对您有所帮助,或者至少能将您推向正确的方向:)

我不认为duck类型应该要求您将调用中的数据映射到对象,只要它们的结构相同。如果去掉
映射(res=>new Champion().from(res))
行会发生什么?什么都不会发生:/I我不认为duck类型应该要求您将调用中的数据映射到对象,只要它们的结构相同。如果去掉
映射(res=>newchampion().from(res))
行,会发生什么呢?什么都不会发生:/fully这是您正在寻找的东西。如果我们需要调整一些东西,让我知道:)太好了!很高兴听到!:):)希望这是你正在寻找的东西。如果我们需要调整一些东西,让我知道:)太好了!很高兴听到!:):)