Click here to Skip to main content
16,015,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get response in following format.
strTokenNumber
={"code":200,"message":"Authorized","data":{"token":1545207965545}}
I am getting excpetion
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[POSInvoiceDetailsIntegration.TokenResult]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

in following statement:
var userauthenticate = JsonConvert.DeserializeObject<UserAuthenticate>(strTokenNumber);


What I have tried:

 public class TokenResult
    {
       //[JsonProperty("TokenResult")]
       public string token { get; set; }
    }
public class UserAuthenticate   
    {
        //TokenResult token = new TokenResult();
        public string code { get; set; }
        public string message { get; set; }

        //public Array data { get; set; }
        //public List<string> data { get; set; }
        public List<TokenResult> data { get; set; }
    }
Posted
Updated 27-Nov-18 0:02am

In the json you have "data" is a single object, not an array\list so change your class definition;

public class UserAuthenticate   
    {
        public string code { get; set; }
        public string message { get; set; }

        public TokenResult data { get; set; }
    }
 
Share this answer
 
Comments
Member 11874411 27-Nov-18 6:26am    
this solved my problem.
The JSON you show doesn't match your classes - the "data" item is not a collection, buut a single instance.
If I take your JSON and feed to to a class generator: json2csharp - generate c# classes from json[^] I get these:
C#
public class Data
{
    public long token { get; set; }
}

public class RootObject
{
    public int code { get; set; }
    public string message { get; set; }
    public Data data { get; set; }
}

If I feed you classes some data, I get what I expect:
UserAuthenticate ua = new UserAuthenticate();
ua.code = "200";
ua.message = "Authorized";
ua.data = new List<TokenResult>();
ua.data.Add(new TokenResult() { token = "1545207965545" });
string json = Newtonsoft.Json.JsonConvert.SerializeObject(ua);
{"code":"200","message":"Authorized","data":[{"token":"1545207965545"}]}
I'd start by checking your data source.
 
Share this answer
 
v2

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