Hello
how can i desirialized the following Json Result comming from AppService (inherit from AbpServiceBase) to my c# object ? {"result":{"isValid":true,"value":0.87},"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}
The problem is that the property in the result has no name {"isValid":true,"value":0.87}
I m using the function esponse.Content.ReadAsAsync<T>
For Json result of where the result is a list of items, it s work because the list of item is in the Json result is in the following format and has a "items" property. {"result":{"items":[1000,1020,1853,186091,9992]},"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}
How can i desirialized a result of single object in my c# object ?
Thanks
4 Answer(s)
-
0
Hi,
You can create a class for {"isValid":true,"value":0.87} for example:
public class MyClass{ public bool IsValid {get; set;} public double Value{get; set;} }
and map your response to AjaxResponse<MyClass>.
-
0
Hi
i have create a class for the response, but how i can desirialized the json to this c# class?
{"result":{"isValid":true,"value":0.87},"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}
This doesnt work MyClass obj = new MyClass(); HttpResponseMessage response = await GetAsync(path); if (response.IsSuccessStatusCode) obj = await response.Content.ReadAsAsync<MyClass>();
-
0
Hi,
Can you try it like this,
obj = await response.Content.ReadAsAsync<AjaxResponse<MyClass>>();
-
0
Hi
my code looks like this:
public async Task<AjaxResponse<List<int>>> GetListOfInts() { AjaxResponse<List<int>> listOfInts = new AjaxResponse<List<int>>(); string path = $"/api/services/app/Orte/GetPLZByLandId?land={land}"; HttpResponseMessage response = await GetAsync(path); var r = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) listOfInts = await response.Content.ReadAsAsync<AjaxResponse<List<int>>>(); else listOfInts = await response.Content.ReadAsAsync<AjaxResponse<List<int>>>(); return listOfInts; }
My Json Result looks like this: {"result":{"items":[1000,1020,1030,1040,1050,9992]},"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}
On Error the Json looks like this: {"result":null,"targetUrl":null,"success":false,"error":{"code":0,"message":"[Invalid country]","details":null,"validationErrors":null},"unAuthorizedRequest":false,"__abp":true}
But the conversion of the to case doesnt work.