use*_*076 15 c# json json.net deserialization json-deserialization
我从一个看起来像这样的API获得JSON:
{
"Items": {
"Item322A": [{
"prop1": "string",
"prop2": "string",
"prop3": 1,
"prop4": false
},{
"prop1": "string",
"prop2": "string",
"prop3": 0,
"prop4": false
}],
"Item2B": [{
"prop1": "string",
"prop2": "string",
"prop3": 14,
"prop4": true
}]
},
"Errors": ["String"]
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些方法来在c#对象中表示这个JSON(这里列出的太多了).我已尝试过列表和词典,这是我最近尝试如何表示它的一个例子:
private class Response
{
public Item Items { get; set; }
public string[] Errors { get; set; }
}
private class Item
{
public List<SubItem> SubItems { get; set; }
}
private class SubItem
{
public List<Info> Infos { get; set; }
}
private class Info
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public int Prop3 { get; set; }
public bool Prop4 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我用来反序列化JSON的方法:
using (var sr = new StringReader(responseJSON))
using (var jr = new JsonTextReader(sr))
{
var serial = new JsonSerializer();
serial.Formatting = Formatting.Indented;
var obj = serial.Deserialize<Response>(jr);
}
Run Code Online (Sandbox Code Playgroud)
obj包含Items和Errors.而Items含有SubItems,但SubItems是null.因此,除了Errors实际上没有反序列化.
它应该很简单,但由于某种原因,我无法弄清楚正确的对象表示
R.Y*_*You 19
使用此网站进行表示:
这样的事可能会对你有所帮助
public class Item322A
{
public string prop1 { get; set; }
public string prop2 { get; set; }
public int prop3 { get; set; }
public bool prop4 { get; set; }
}
public class Item2B
{
public string prop1 { get; set; }
public string prop2 { get; set; }
public int prop3 { get; set; }
public bool prop4 { get; set; }
}
public class Items
{
public List<Item322A> Item322A { get; set; }
public List<Item2B> Item2B { get; set; }
}
public class jsonObject
{
public Items Items { get; set; }
public List<string> Errors { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以下是反序列化的方法(使用JsonConvert类):
jsonObject ourlisting = JsonConvert.DeserializeObject<jsonObject>(strJSON);
Run Code Online (Sandbox Code Playgroud)
dbc*_*dbc 14
对于"Items"使用Dictionary<string, List<Info>>,即:
class Response
{
public Dictionary<string, List<Info>> Items { get; set; }
public string[] Errors { get; set; }
}
class Info
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public int Prop3 { get; set; }
public bool Prop4 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是假设该项目的名称"Item322A",并"Item2B"从响应有所不同响应,并作为字典键读取这些名称.
样品小提琴.
小智 6
您可以使用,Json.Parse以便您可以查询数据——并且只使用单个模型。
private class Info
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public int Prop3 { get; set; }
public bool Prop4 { get; set; }
}
var result = JObject.Parse(resultContent); //parses entire stream into JObject, from which you can use to query the bits you need.
var items = result["Items"].Children().ToList(); //Get the sections you need and save as enumerable (will be in the form of JTokens)
List<Info> infoList = new List<Info>(); //init new list to store the objects.
//iterate through the list and match to an object. If Property names don't match -- you could also map the properties individually. Also useful if you need to dig out nested properties.
foreach(var subItem in items){
foreach(JToken result in subItem){
Info info = result.ToObject<Info>();
infoList.add(info);
}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37719 次 |
| 最近记录: |