我有一个包含多个对象数组的JSON文件:
{
"projects":[{
"issuetypes":[{
"fields":{
"priority":{
"allowedValues":[{
"self":"",
"iconUrl":"",
"name":"",
"id":"1"
}]
},
"components":{
"allowedValues":[{
"self":"",
"id":"",
"name":""
}
然后我试图将其解析为C#类。
Google仅提供示例,其中包含简化版本的反序列化JSON do C#对象。
我现在创建了什么?
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 ...
答案 0 :(得分:0)
有些工具可以帮助您从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; }
}