C# 反序列化JSON,其中值是JSON字符串
我使用这个解决方案来反序列化我的JSON字符串。如果可能的话,我会尽量避免使用第三方库 但是,我使用的JSON字符串的结构如下:C# 反序列化JSON,其中值是JSON字符串,c#,json,deserialization,json-deserialization,C#,Json,Deserialization,Json Deserialization,我使用这个解决方案来反序列化我的JSON字符串。如果可能的话,我会尽量避免使用第三方库 但是,我使用的JSON字符串的结构如下: {"BatchId":"32","BatchPriority":"NORMAL","Entries":{"Entry": [{"EntryId":"185","EntryPriority":"HIGH","Value":"0.0"}, {"EntryId":"172","EntryPriority":"LOW","Value":"122.0"}] }
{"BatchId":"32","BatchPriority":"NORMAL","Entries":{"Entry":
[{"EntryId":"185","EntryPriority":"HIGH","Value":"0.0"},
{"EntryId":"172","EntryPriority":"LOW","Value":"122.0"}]
}}
我能够反序列化JSON,将BatchId
和BatchPriority
设置为Batch
对象(见下文)。但是,我无法设置JSON中的“条目”数组,其值似乎是JSON字符串本身
我如何构造我的对象,以便我能够反序列化并将“条目”数组设置到Batch
对象?我能够设置BatchId
和BatchPriority
属性的值?我知道公共数组条目{get;set;}
是错误的,但我不确定如何处理
批处理
对象:
public class Batch
{
public int BatchId { get; set; }
public string BatchPriority { get; set; }
public Array Entries { get; set; } //wrong
}
public class Entry
{
public int EntryId { get; set; }
public string EntryPriority { get; set; }
public double Value { get; set; }
}
条目
对象:
public class Batch
{
public int BatchId { get; set; }
public string BatchPriority { get; set; }
public Array Entries { get; set; } //wrong
}
public class Entry
{
public int EntryId { get; set; }
public string EntryPriority { get; set; }
public double Value { get; set; }
}
您可以创建一个包含
列表的类
公共类条目
{
公共列表项{get;set;}
}
如果您查看,它将为您生成类。您的属性
公共数组条目{get;set;}
是一个系统。数组
称为条目,您需要一个名为Entry的条目类数组或集合,例如
public class Batch
{
public int BatchId { get; set; }
public string BatchPriority { get; set; }
public Entry[] Entry { get; set; }
}