简体   繁体   English

json 到 C# 对象如何反序列化 JSON

[英]json to C# object how to deserialize JSON

How to deserialize this json string in C#如何在 C# 中反序列化这个 json 字符串

{ "accountant": { "sysid": "1", "first_name": "Test", "last_name": "Test", "product_type": "Sample" } } { “会计师”:{ “sysid”:“1”,“first_name”:“Test”,“last_name”:“Test”,“product_type”:“样本”} }

What I tried so far is using JsonConvert.DeserializeObject到目前为止我尝试过的是使用JsonConvert.DeserializeObject

var jsonObj = JsonConvert.DeserializeObject<AccountantData>(responseMessage);

this is my Model这是我的模特

public class AccountantData
{
    [JsonProperty("accountant")]
    public List<UserData> Accountant { get; set; }
}
public class UserData
{
    public UserData(int sysId, string firstName, string lastName, string productType)
    {
        SystemId = sysId;
        FirstName = firstName;
        LastName = lastName;
        ProductType = productType;
    }

    [JsonProperty("sysid")]
    public int SystemId { get; set; }

    [JsonProperty("first_name")]
    public string FirstName { get; set; }

    [JsonProperty("last_name")]
    public string LastName { get; set; }

    [JsonProperty("product_type")]
    public string ProductType { get; set; }
}

but this results to an error但这会导致错误

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code Newtonsoft.Json.dll 中发生了“Newtonsoft.Json.JsonSerializationException”类型的异常,但未在用户代码中处理

Additional information: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[MyApp.UserData]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.附加信息:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“System.Collections.Generic.List`1[MyApp.UserData]”,因为该类型需要 JSON 数组(例如 [1 ,2,3]) 以正确反序列化。

To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object.要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为正常的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型)可以从 JSON 对象反序列化的数组或列表)。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。

Your JSON input string is not correct.您的 JSON 输入字符串不正确。 AccountantData uses List (Array) of UserData . AccountantData使用UserData List (数组)。 Try this:尝试这个:

{
   "accountant":[
      {
         "sysid":"1",
         "first_name":"Test",
         "last_name":"Test",
         "product_type":"Sample"
      }
   ]
}

Update: If you are always going to have a single object of UserData then you can change your AccountantData class as:更新:如果您总是要拥有一个UserData对象,那么您可以将AccountantData类更改为:

public class AccountantData
{
  [JsonProperty("accountant")]
  UserData Accountant { get; set; }
}

Also, in case you will always have single UserData object and you have control of JSON data, then you can update your JSON input data to simpler format as below:此外,如果您始终只有一个UserData对象并且您可以控制 JSON 数据,那么您可以将 JSON 输入数据更新为更简单的格式,如下所示:

{
   "sysid":"1",
   "first_name":"Test",
   "last_name":"Test",
   "product_type":"Sample"
}

and then deserialize it like this:然后像这样反序列化它:

var jsonObj = JsonConvert.DeserializeObject<UserData>(responseMessage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM