简体   繁体   中英

Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.ICollection`1[System.Double]'

As i post above, i can't make it through. Im not sure how should like implementation of this, because i'm pretty new at front-end. At first i post example back-end JSON file (i have problem with borderPoints variable, before i add it, everything works fine).

{ 
city: "Warszawa", 
id: "b7e476a5-5894-4510-9630-637ac3a2191d", 
name: "Test", 
parcels: [{ 
id: "b7e476a5-5894-4510-9630-637ac3a2191d", 
latitude: 0, 
longitude: 0,   
"borderCoordinates": [
                [
                    0,
                    0
                ],
                [
                    1,
                    1
                ],
                [
                    1,
                    0
                ]
            ], }], }

I post bellow some important parts of code.

land.model.ts:

...
export class LocationPoint {
  public latitude: number;
  public longitude: number;
}
export class LocationPointDto {
  public latitude: number;
  public longitude: number;
}
export class Land {
...
  public id: Guid;
  public borderCoordinates: LocationPoint[];
}
export class LandDto {
...
  public id: string;
  public borderCoordinates: LocationPoint[];
  constructor(
...
    id: string,
    borderCoordinates: LocationPoint[]
  ) {
...
    this.id = id;
    this.borderCoordinates = borderCoordinates;
  }
}
...

land.module.ts

...
export function initializeMappings(mappingService: MappingService): any {
  return (): void => {
    mappingService.addMapping(LocationPoint, LocationPointDto, new BorderCoordinatesMapper());
    mappingService.addMapping(Land, LandDto, new LandMapper());
...

land-builder.ts

...
  public withborderCoordinates(borderCoordinates: LocationPoint[]): LandBuilder {
    this.instance.borderCoordinates = borderCoordinates;
    return this;
  }
...

land.mapper.ts

...
export class BorderCoordinatesMapper implements Mapper<LocationPoint, LocationPointDto> {
  public forwardTransform(locationPoint: LocationPoint): LocationPointDto {
    return {
      latitude: locationPoint.latitude,
      longitude: locationPoint.longitude
    };
  }

  public backwardTransform(locationPointDto: LocationPointDto): LocationPoint {
    return {
      latitude: locationPointDto.latitude,
      longitude: locationPointDto.longitude
    };
  }
}

export class LandMapper implements Mapper<Land, LandDto> {
  public forwardTransform(land: Land, mappingService: MappingService): LandDto {
    return {
      id: land.id.toString(),
      borderCoordinates: { [key: string]: land.borderCoordinates}
         ? land.borderCoordinates.map(p => mappingService.map(LocationPoint, LocationPointDto, p))
         : []
    };
  }

  public backwardTransform(land: LandDto, mappingService: MappingService): Land {
    return {
      id: Guid.parse(land.id),
      borderCoordinates: land.borderCoordinates
         ? land.borderCoordinates.map(p => mappingService.map(LocationPointDto, LocationPoint, p))
         : []
    };
  }
}
...

Try this in your json:

"borderCoordinates": [
            {
                "latitude": 0,
                "longitude"0
            },
            {
                "latitude": 1,
                "longitude": 1
            },
            {
                "latitude": 1,
                "longitude": 0
            }
        ]

我仍然没有找到解决我问题的方法……这很可悲。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM