arrays - 通过 json2typescript 反序列化 json 时出错

标签 arrays angular typescript object json-deserialization

我正在尝试反序列化我的 JSON 以使用库 json2typescript 在 typescript 中进行转换但我有以下错误:

Error: Fatal error in JsonConvert. Failed to map the JSON object to the class "typeGeneral" because the defined JSON property "type" does not exist:


Class property: 
    _type

JSON property: 
    type

service.ts

   return this.http.get<Type>(this.applicationApiUrl + "/app", this.httpOptions)
     .pipe(retry(1), catchError(this.handleError));
 }

模型.ts
import {JsonObject, JsonProperty} from "json2typescript";


@JsonObject("app")
export class App {


  constructor(id: number, title: string, url: any, details: string, type: string, visible: boolean, imgApp: any, kazanUrl: any, favorite: boolean) {
    this._id = id;
    this._title = title;
    this._url = url;
    this._details = details;
    this._type = type;
    this._visible = visible;
    this._imgApp = imgApp;
    this._kazanUrl = kazanUrl;
    this._favorite = favorite;
  }

  private _id: number = null;


  @JsonProperty("title", String)
  private _title: string = undefined;


  @JsonProperty("url")
  private _url: any = undefined;


  @JsonProperty("details", String)
  private _details: string = undefined;


  @JsonProperty("type", String)
  private _type: string = "";


  @JsonProperty("visible", Boolean)
  private _visible: boolean = false;


  @JsonProperty("imgApp")
  private _imgApp: any = undefined;


  @JsonProperty("kazanUrl")
  private _kazanUrl: any = undefined;


  @JsonProperty("favorite", Boolean)
  private _favorite: boolean;


  get id(): number {
    return this._id;
  }

  set id(value: number) {
    this._id = value;
  }

  get title(): string {
    return this._title;
  }

  set title(value: string) {
    this._title = value;
  }

  get url(): any {
    return this._url;
  }

  set url(value: any) {
    this._url = value;
  }

  get details(): string {
    return this._details;
  }

  set details(value: string) {
    this._details = value;
  }

  get type(): string {
    return this._type;
  }

  set type(value: string) {
    this._type = value;
  }

  get visible(): boolean {
    return this._visible;
  }

  set visible(value: boolean) {
    this._visible = value;
  }

  get imgApp(): any {
    return this._imgApp;
  }

  set imgApp(value: any) {
    this._imgApp = value;
  }

  get kazanUrl(): any {
    return this._kazanUrl;
  }

  set kazanUrl(value: any) {
    this._kazanUrl = value;
  }

  get favorite(): boolean {
    return this._favorite;
  }

  set favorite(value: boolean) {
    this._favorite = value;
  }



}
@JsonObject("typeGeneral")
export class Type {



  private _id: number = null;

  @JsonProperty("type", String) private _type: string = "";

  @JsonProperty("details", String) private _details: string = undefined;

  @JsonProperty("visible", Boolean) private _visible: boolean = null;

  @JsonProperty("app", [App]) private _app: [App] = undefined;


  get app(): any {
    return this._app;
  }

  set app(value: any) {
    this._app = value;
  }

  get id(): number {
    return this._id;
  }

  set id(value: number) {
    this._id = value;
  }

  get type(): string {
    return this._type;
  }

  set type(value: string) {
    this._type = value;
  }

  get details(): string {
    return this._details;
  }

  set details(value: string) {
    this._details = value;
  }

  get visible(): boolean {
    return this._visible;
  }

  set visible(value: boolean) {
    this._visible = value;
  }
}


组件.ts

  loadApplications() {
    return this.restApi.getApplications()
      .subscribe((data: {}) => {

       console.log(data)

        let jsonStr: string = JSON.stringify(data);
        const jsonObjApp: any = JSON.parse(jsonStr);

        let jsonConvert: JsonConvert = new JsonConvert();
        jsonConvert.operationMode = OperationMode.LOGGING;
        jsonConvert.ignorePrimitiveChecks = false;
        jsonConvert.valueCheckingMode = ValueCheckingMode.DISALLOW_NULL;

        console.log(jsonObjApp)

        try {
          let a: Type[] = jsonConvert.deserializeArray(jsonObjApp, Type);

        } catch (e) {
          console.log((<Error>e))
        }
      });


这是我的 JSON
[
    {
        "typeGeneral":[
            {
                "id":1,
                "type":"Monitoring",
                "details":"Ceci est un type Monitoring",
                "visible":true
            },
            {
                "id":2,
                "type":"Check service",
                "details":"Ceci est un type Check Service",
                "visible":false
            },
            {
                "app":[
                {
                    "id":1,
                    "type":"Monitoring",
                    "visible":false,
                    "imgApp":"http://google.fr",
                    "details":"Lorem ipsum dolor",
                    "kazanUrl":"http://google.fr",
                    "favorite":false,
                    "title":"Monitoring app",
                    "url":"http://google.fr"
                },
                {
                    "id":2,
                    "type":"Check Service",
                    "visible":true,
                    "imgApp":"http://google.fr",
                    "details":"Lorem ipsum dolor",
                    "kazanUrl":"http://google.fr",
                    "favorite":true,
                    "title":"Services app",
                    "url":"http://google.fr"
                }
                ]
            }
        ]
    }
]

如何解决这个问题?我被卡住了:(

最佳答案

@JsonProperty可能有3个参数。您需要将第三个设置为 true .@JsonProperty("title", String, true)第三个参数告诉属性是否可以是可选的。 [https://www.npmjs.com/package/json2typescript]
您可以通过从 json2typescript 包中导入 Any 来使用它。import {JsonObject, JsonProperty, Any} from "json2typescript";@JsonProperty("url", Any, true)

关于arrays - 通过 json2typescript 反序列化 json 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57289515/

相关文章:

javascript - 将字符串按照指定长度分割成数组

c# - 最大数组维度,如 a[1][1][1][1]....[1]在 C#

unit-testing - 如何在 Jasmine 中通过类名获取 Angular 2 元素

angularjs - 没有模板/模板的 Angular 2 bootstrap 应用程序组件由 html 提供

ios - 角标(Badge)数量始终为1(云函数)

typescript - (TypeScript2) 你如何遍历接口(interface)类型的数组?

javascript - 如何使用数组触发 touchevents?

python - 稀疏数组的 numpy 向量列表

angular - CanDeactivate 确认消息

javascript - 编译到 wwwroot 中的 Typescript 给出 "Duplicate identifier"错误