[英]How to deserialize json into collection of Objects
我有以下JSON(我無法更改傳入的JSON,因為它是來自第三方系統):
{
"id": 23,
"userName":"test@test.com",
"tags":
{
"Employee ID":
{
"name":"Employee ID",
"value":"123456789"
},
"Job Family":
{
"name": "Job Family",
"value": "Accounting and Finance"
}
}
}
首先,我嘗試使用以下類結構進行反序列化:
public class User
{
[JsonProperty("id")]
public int ID { get; set; }
[JsonProperty("username")]
public string Email { get; set; }
[JsonProperty("tags")]
public TagsJson Tags { get; set; }
}
public class TagsJson
{
[JsonProperty("tags")]
public List<Tag> Tags { get; set; }
}
public class Tag
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
在使用Newtonsoft的JsonConvert.DeserializeObject(json);
,User.Tags屬性始終為空。 顯然,這是因為在“標簽”屬性下沒有“標簽”。
但是,如果我按如下方式更改User類...
public class User
{
[JsonProperty("id")]
public int ID { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("tags")]
public List<Tag> Tags { get; set; }
// I even tried Tag[] instead of List<Tag> here
}
...我收到以下錯誤:
附加信息:無法將當前JSON對象(例如{“ name”:“ value”})反序列化為類型'System.Collections.Generic.List`1 [Tag]',因為該類型需要JSON數組(例如[1,2 ,3])正確反序列化。
非常感謝您提供有關創建User類以使其正確反序列化的任何建議。
編輯
因此,這可能是最佳答案,也可能不是最佳答案。 進入的標簽將是十二種不同名稱之一。 因此,我為12個可能性中的每一個創建了一個具有屬性的Tags類。 由於不需要任何內容,因此會顯示確實出現的任何內容。 這是我經過調整的用戶,下面的Tag(以前稱為TagsJson)和Tag類-但我絕對對更好的解決方案感興趣。
public class User
{
[JsonProperty("id")]
public int ID { get; set; }
[JsonProperty("username")]
public string Email { get; set; }
[JsonProperty("tags")]
public Tags AllTags { get; set; }
}
public class Tags
{
[JsonProperty("Employee ID")]
public Tag EmployeeID { get; set; }
[JsonProperty("Job Family")]
public Tag JobFamily { get; set; }
// ... and 10 more properties for additional tags that may appear but are not required
}
public class Tag
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
在我的情況下, 此問題的建議答案不起作用,因為JSON不是數組或集合,而是具有名稱/值對的唯一屬性名稱的列表。
不確定JSON是否正確,但是為什么您的財產名稱Employee ID
或Job Family
有空間。 修復Tag類中的問題,我們很好。
public class EmployeeID
{
public string name { get; set; }
public string value { get; set; }
}
public class JobFamily
{
public string name { get; set; }
public string value { get; set; }
}
public class Tags
{
public EmployeeID __invalid_name__Employee ID { get; set; }
public JobFamily __invalid_name__Job Family { get; set; }
}
public class User
{
public int id { get; set; }
public string userName { get; set; }
public Tags tags { get; set; }
}
這是我從json2csharp.com得到的
您的JSON表示您的tags
對象是一個Object
-而不是Array
。 您不能將Object
反序列化為C# List
因為它們是不同的結構。
如果tags
中的鍵是動態的/正在更改,則可以嘗試
[JsonProperty("tags")]
Dictionary<string, string> Tags { get; set; }
看來您的JSON格式不正確; 如果您無法修改它,則可能必須使用自定義JsonConverter 。
很好地使用Visual Studio將Json粘貼為類產生...
public class Rootobject
{
public int id { get; set; }
public string userName { get; set; }
public Tags tags { get; set; }
}
public class Tags
{
[JsonProperty(PropertyName = "Employee ID")]
public EmployeeID EmployeeID { get; set; }
[JsonProperty(PropertyName = "Job Family")]
public JobFamily JobFamily { get; set; }
}
public class EmployeeID
{
public string name { get; set; }
public string value { get; set; }
}
public class JobFamily
{
public string name { get; set; }
public string value { get; set; }
}
我必須自己添加JsonProperty屬性。 但是我認為更好的解決方案是...
public class Rootobject2
{
public int id { get; set; }
public string userName { get; set; }
public IDictionary<string, NameValuePair> tags { get; set; }
}
public class NameValuePair
{
public string name { get; set; }
public string value { get; set; }
}
盡管將Json作為類粘貼是輕松復制和粘貼代碼到首選解決方案中的一個很好的起點。
一些測試代碼...
string json = Resource1.String1;
Rootobject test = JsonConvert.DeserializeObject<Rootobject>(json);
Rootobject2 test2 = JsonConvert.DeserializeObject<Rootobject2>(json);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.