Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

convert Json result with single object to c# object #2301


User avatar
0
michaelhilgers created

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)
  • User Avatar
    0
    ismcagdas created
    Support Team

    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>.

  • User Avatar
    0
    michaelhilgers created

    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>();

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you try it like this,

    obj = await response.Content.ReadAsAsync<AjaxResponse<MyClass>>();
    
  • User Avatar
    0
    michaelhilgers created

    Hi

    my code looks like this:

        public async Task&lt;AjaxResponse&lt;List&lt;int&gt;>> GetListOfInts()
        {
            AjaxResponse&lt;List&lt;int&gt;> listOfInts = new AjaxResponse&lt;List&lt;int&gt;>();
    
            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&lt;AjaxResponse&lt;List&lt;int&gt;>>();
            else
                listOfInts = await response.Content.ReadAsAsync&lt;AjaxResponse&lt;List&lt;int&gt;>>();
    
            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.