将 JSON 数据解析为 C# 对象
Parsing JSON data to C# Object
我有 JSON 个包含多个对象数组的文件:
{
"projects":[{
"issuetypes":[{
"fields":{
"priority":{
"allowedValues":[{
"self":"",
"iconUrl":"",
"name":"",
"id":"1"
}]
},
"components":{
"allowedValues":[{
"self":"",
"id":"",
"name":""
}
我尝试将其解析为 C# class。
Google 只有简化版本的反序列化示例 JSON do C# Object.
我现在创造了什么?
public class RootObject
{
[JsonProperty(PropertyName = "projects")]
public List<ProjectObject> Projects { get; set; }
}
public class ProjectObject
{
[JsonProperty(PropertyName = "issueTypes")]
public List<IssueObject> Issues { get; set; }
}
public class IssueObject
{
[JsonProperty(PropertyName = "fields")]
public FieldObject Field { get; set; }
}
public class FieldObject
{
[JsonProperty(PropertyName = "components")]
public ComponentObject Component { get; set; }
[JsonProperty(PropertyName = "priority")]
public PriorityObject Priority { get; set; }
}
public class PriorityObject
{
[JsonProperty(PropertyName = "value")]
public PriorityAllowedValues PriorityValues { get; set; }
}
public class PriorityAllowedValues
{
public List<PriorityValues> AllowedValues { get; set; }
}
public class PriorityValues
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "self")]
public string Self { get; set; }
[JsonProperty(PropertyName = "iconUrl")]
public string IconUrl { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}
public class ComponentObject
{
[JsonProperty(PropertyName = "value")]
public ComponentAllowedValues ComponentAllowedValues { get; set; }
}
public class ComponentAllowedValues
{
public List<SelectObject> AllowedValues { get; set; }
}
public class SelectObject
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "self")]
public string Self { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}
当我尝试执行这一行时:
RootObject root = JsonConvert.DeserializeObject<RootObject>(data);
来自 JSON 文件的数据应该正确地传送到 RootObject...
有一些工具可以帮助您从 JSON 生成 类,例如https://jsonutils.com/
此结构适用于提供的 json:
public class AllowedValue
{
[JsonProperty("self")]
public string Self { get; set; }
[JsonProperty("iconUrl")]
public string IconUrl { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
}
public class Priority
{
[JsonProperty("allowedValues")]
public IList<AllowedValue> AllowedValues { get; set; }
}
public class AllowedValue
{
[JsonProperty("self")]
public string Self { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public class Components
{
[JsonProperty("allowedValues")]
public IList<AllowedValue> AllowedValues { get; set; }
}
public class Fields
{
[JsonProperty("priority")]
public Priority Priority { get; set; }
[JsonProperty("components")]
public Components Components { get; set; }
}
public class Issuetype
{
[JsonProperty("fields")]
public Fields Fields { get; set; }
}
public class Project
{
[JsonProperty("issuetypes")]
public IList<Issuetype> Issuetypes { get; set; }
}
public class RootObject
{
[JsonProperty("projects")]
public IList<Project> Projects { get; set; }
}
我有 JSON 个包含多个对象数组的文件:
{
"projects":[{
"issuetypes":[{
"fields":{
"priority":{
"allowedValues":[{
"self":"",
"iconUrl":"",
"name":"",
"id":"1"
}]
},
"components":{
"allowedValues":[{
"self":"",
"id":"",
"name":""
}
我尝试将其解析为 C# class。
Google 只有简化版本的反序列化示例 JSON do C# Object.
我现在创造了什么?
public class RootObject
{
[JsonProperty(PropertyName = "projects")]
public List<ProjectObject> Projects { get; set; }
}
public class ProjectObject
{
[JsonProperty(PropertyName = "issueTypes")]
public List<IssueObject> Issues { get; set; }
}
public class IssueObject
{
[JsonProperty(PropertyName = "fields")]
public FieldObject Field { get; set; }
}
public class FieldObject
{
[JsonProperty(PropertyName = "components")]
public ComponentObject Component { get; set; }
[JsonProperty(PropertyName = "priority")]
public PriorityObject Priority { get; set; }
}
public class PriorityObject
{
[JsonProperty(PropertyName = "value")]
public PriorityAllowedValues PriorityValues { get; set; }
}
public class PriorityAllowedValues
{
public List<PriorityValues> AllowedValues { get; set; }
}
public class PriorityValues
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "self")]
public string Self { get; set; }
[JsonProperty(PropertyName = "iconUrl")]
public string IconUrl { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}
public class ComponentObject
{
[JsonProperty(PropertyName = "value")]
public ComponentAllowedValues ComponentAllowedValues { get; set; }
}
public class ComponentAllowedValues
{
public List<SelectObject> AllowedValues { get; set; }
}
public class SelectObject
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "self")]
public string Self { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}
当我尝试执行这一行时:
RootObject root = JsonConvert.DeserializeObject<RootObject>(data);
来自 JSON 文件的数据应该正确地传送到 RootObject...
有一些工具可以帮助您从 JSON 生成 类,例如https://jsonutils.com/ 此结构适用于提供的 json:
public class AllowedValue
{
[JsonProperty("self")]
public string Self { get; set; }
[JsonProperty("iconUrl")]
public string IconUrl { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
}
public class Priority
{
[JsonProperty("allowedValues")]
public IList<AllowedValue> AllowedValues { get; set; }
}
public class AllowedValue
{
[JsonProperty("self")]
public string Self { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public class Components
{
[JsonProperty("allowedValues")]
public IList<AllowedValue> AllowedValues { get; set; }
}
public class Fields
{
[JsonProperty("priority")]
public Priority Priority { get; set; }
[JsonProperty("components")]
public Components Components { get; set; }
}
public class Issuetype
{
[JsonProperty("fields")]
public Fields Fields { get; set; }
}
public class Project
{
[JsonProperty("issuetypes")]
public IList<Issuetype> Issuetypes { get; set; }
}
public class RootObject
{
[JsonProperty("projects")]
public IList<Project> Projects { get; set; }
}