添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.'

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
000067

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

"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"

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.