1

JSON

{
"SoftHoldIDs": 444,
"AppliedUsages": [
    {
        "SoftHoldID": 444,
        "UsageYearID": 223232,
        "DaysApplied": 0,
        "PointsApplied": 1
    }
],
"Guests": [
    1,
    2
]

} In the above JSON SoftholdIDs is integer and AppliedUsages is class array property in C# Model

Issue is --How we can map JSON to class property. Class code

  public class ReservationDraftRequestDto
{

    public int SoftHoldIDs { get; set; }
    public int[] Guests { get; set; }
    public AppliedUsage[] AppliedUsages { get; set; }

}


public class AppliedUsage
{
    public int SoftHoldID { get; set; }
    public int UsageYearID { get; set; }
    public int DaysApplied { get; set; }
    public int PointsApplied { get; set; }
}

Tried below code for mapping

ReservationDraftRequestDto reservationDto = null;

        dynamic data  = await reservationDraftRequestDto.Content.ReadAsAsync<object>();
                    reservationDto = JsonConvert.DeserializeObject<ReservationDraftRequestDto>(data.ToString());

2 Answers 2

2

You need to change

dynamic data  = await reservationDraftRequestDto.Content.ReadAsAsync<object>();

to

string data = await reservationDraftRequestDto.Content.ReadAsStringAsync();

this will read your response as string

then do

reservationDto = JsonConvert.DeserializeObject<ReservationDraftRequestDto>(data);
Sign up to request clarification or add additional context in comments.

4 Comments

not working---Getting error Exception while executing function: Function1. Newtonsoft.Json: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'testazfunction.AppliedUsage' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Try changing public int[] Guests { get; set; } to public List<int> Guests { get; set; } and public AppliedUsage[] AppliedUsages { get; set; } to public List<AppliedUsage> AppliedUsages { get; set; } in your DTO's
Have you checked the actual json that is coming from the server, maybe it is malformed?
0

this work

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
         class Program
    {
        static void Main(string[] args)
        {
            string json = @"{""SoftHoldIDs"": 444,""AppliedUsages"": [    {""SoftHoldID"": 444,""UsageYearID"": 223232,""DaysApplied"": 0,""PointsApplied"": 1}],""Guests"": [ 1, 2]}";

            Rootobject reservationDto = JsonConvert.DeserializeObject<Rootobject>(json.ToString());

            Debug.WriteLine(reservationDto.SoftHoldIDs);
            foreach (var guest in reservationDto.Guests)
            {

                Debug.WriteLine(guest);
            }

        }
    }

    public class Rootobject
    {
        public int SoftHoldIDs { get; set; }
        public Appliedusage[] AppliedUsages { get; set; }
        public int[] Guests { get; set; }
    }

    public class Appliedusage
    {
        public int SoftHoldID { get; set; }
        public int UsageYearID { get; set; }
        public int DaysApplied { get; set; }
        public int PointsApplied { get; set; }
    }


}

First create class copying json as classwith visualstudio. Next you have double quote in json respons so deal with it. Json.NET: Deserilization with Double Quotes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.