Home > other >  Newtonsoft.Json.JsonSerializationException: C#
Newtonsoft.Json.JsonSerializationException: C#

Time:03-21

Good Day!

I'm have a problem in deserializing JSON string

This is my function look like:

 public static void Deserialised()
    {
        string jsonString = "{\"data\":{\"merchantRefNo\":\"foo\","  
                            "\"responseText\":\"Approved\","  
                            "\"status\":\"A\","  
                            "\"txnDate\":\"20220321\","  
                            "\"txnId\":\"000067\","  
                            "\"txnTime\":\"1049\","  
                            "\"txnType\":\"sale\","  
                            "\"amt\":\"109\"},"  
                            "\"dataType\":\"trans\"}";

        Root myDeserializedJson = JsonConvert.DeserializeObject<Root>(jsonString); //Error popup here!

        foreach (var datas in myDeserializedJson.data)
        {
            Console.WriteLine(datas.merchantRefNo);
        }
    }

This is my Model:

 public class Data
    {
        public string merchantRefNo { get; set; }
        public string responseText { get; set; }
        public string status { get; set; }
        public string txnDate { get; set; }
        public string txnId { get; set; }
        public string txnTime { get; set; }
        public string txnType { get; set; }
        public string amt { get; set; }
    }

    public class Root
    {
        public List<Data> data { get; set; }
        public string dataType { get; set; }
    }

Error:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[fortestings.Program Data]' 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) 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. Path 'data.merchantRefNo', line 1, position 25.'

CodePudding user response:

In your Root class, change the List<Data> data to Data data and then you can de-serialize your JSON string as required to your Root class:

using System;
using Newtonsoft.Json;
                    
public class Program
{
    public static void Main()
    {
        string jsonString = "{\"data\":{\"merchantRefNo\":\"foo\","  
            "\"responseText\":\"Approved\","  
            "\"status\":\"A\","  
            "\"txnDate\":\"20220321\","  
            "\"txnId\":\"000067\","  
            "\"txnTime\":\"1049\","  
            "\"txnType\":\"sale\","  
            "\"amt\":\"109\"},"  
            "\"dataType\":\"trans\"}";
        
        var result =JsonConvert.DeserializeObject<Root>(jsonString);
        Console.WriteLine(result.dataType);
        Console.WriteLine(result.data.merchantRefNo);
        Console.WriteLine(result.data.status);
        Console.WriteLine(result.data.txnId);           
    }
}

public class Data
{
    public string merchantRefNo { get; set; }
    public string responseText { get; set; }
    public string status { get; set; }
    public string txnDate { get; set; }
    public string txnId { get; set; }
    public string txnTime { get; set; }
    public string txnType { get; set; }
    public string amt { get; set; }
}

public class Root
{
    public Data data { get; set; }
    public string dataType { get; set; }
}

Output:

trans
foo
A
000067

You can find a working example here: https://dotnetfiddle.net/8Nsl9m

CodePudding user response:

{ "data":{ "merchantRefNo":"foo", "responseText":"Approved", "status":"A", "txnDate":"20220321", "txnId":"000067", "txnTime":"1049", "txnType":"sale", "amt":"109" }, "dataType":"trans" }

This is your root object JSON but you class is expecting a list of data

public class Root
{
    public **List<Data>** data { get; set; }
    public string dataType { get; set; }
}

So for deserializing json is not matching with your model. Your Json should be like below { "data":[{ "merchantRefNo":"foo", "responseText":"Approved", "status":"A", "txnDate":"20220321", "txnId":"000067", "txnTime":"1049", "txnType":"sale", "amt":"109" }], "dataType":"trans" }

  • Related