Click here to Skip to main content
14,935,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want to deserialize json data.

my class is fllowing way.

C#
public class ServiceAgreementKey
{
    public string abc{ get; set; }
   
}

public class ServiceAgreementAddtionKey
{
    public string 123{ get; set; }
    
}

public class ServiceAgreementAddition
{
    public ServiceAgreementAddtionKey serviceAgreementAddtionKey { get; set; }
    public int price{ get; set; }
    
}

public class ServiceKit
{
    public string Number { get; set; }
}

public class ServiceOccasion
{
    public string serviceOccasionDate { get; set; }
    public List<servicekit> serviceKits { get; set; }
}

public class RootObject
{
    public ServiceAgreementKey serviceAgreementKey { get; set; }
    public List<serviceagreementaddition> serviceAgreementAdditions { get; set; }
    public List<serviceoccasion> serviceOccasions { get; set; }


What I have tried:

C#
RootObject dataObjects = response.Content.ReadAsAsync<rootobject>().Result;


i am getting followimng error:

{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1 because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo 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}

can any onr help on this...

thanks...
Posted
Updated 21-Oct-16 6:54am
Comments
F-ES Sitecore 20-Oct-16 7:32am
   
The error is self-explanatory, the type you are trying to deserialise to can't be mapped to the JSON you are using. Seeing as you haven't posted the JSON data as you don't think it might help people solve your issue, there isn't much we can do.
Nathan Minier 20-Oct-16 7:57am
   
So JSON has 2 types: Object and Array.

Objects contain properties (which can be Arrays).
Arrays contain Objects, but not properties.

You're trying to de-serialize an Object as if it were an Array, which it can't do.

At a guess, looking at the little code you did provide, either the property serviceAgreementAdditions or serviceOccasions has been serialized as an object. Look at the raw JSON, it should be fairly obvious.

1 solution

I've had excellent luck with Newtonsoft for JSON[^]. Its a popular nuget package.

Please consider saving the data you read into a string variable and include the JSON you're trying to deserialize in the question.

Hogan
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900