Reputation: 91
I'm trying to deserialize my JSON to convert in typescript with the library json2typescript
but I have this following error:
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));
}
model.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;
}
}
component.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))
}
});
and here is my 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"
}
]
}
]
}
]
How to resolve this problem? I'm stuck :(
Upvotes: 4
Views: 2213
Reputation: 46
@JsonProperty
may have 3 parameters. You need to set the third one to true
.
@JsonProperty("title", String, true)
The third parameter tells if the property can be optional. [https://www.npmjs.com/package/json2typescript]
You can use Any by importing it from json2typescript package.
import {JsonObject, JsonProperty, Any} from "json2typescript";
@JsonProperty("url", Any, true)
Upvotes: 2