gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:34:16 31 4
gpt4 key购买 nike

我正在尝试反序列化我的 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com