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());
Priti kumari
  • 79
  • 2
  • 12

2 Answers2

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);
MRW
  • 81
  • 1
  • 4
  • 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. – Priti kumari Oct 13 '19 at 14:38
  • 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) 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. – Priti kumari Oct 13 '19 at 14:39
  • Try changing public int[] Guests { get; set; } to public List Guests { get; set; } and public AppliedUsage[] AppliedUsages { get; set; } to public List AppliedUsages { get; set; } in your DTO's – MRW Oct 13 '19 at 14:43
  • Have you checked the actual json that is coming from the server, maybe it is malformed? – MRW Oct 13 '19 at 14:46
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

Zwan
  • 632
  • 2
  • 6
  • 23