必须从API调用反序列化JSON。在下面发布JSON流。必须获得每个“区域”节点。请注意,有些孩子有孩子(这是我遇到了麻烦)。昨天之前甚至不知道JSON是什么(说我已经出去了一段时间)。
我发现了什么: 添加了Netwonsoft.Json参考 将其转换为XML以查看是否有所帮助,但认为保持JSON会有更好的性能。
vb.net类具有以下属性: Area_Name为字符串(JSON中的“ID”) Area_ID为字符串(JSON中的“Name”) Parent_ID为字符串(JSON中的父“ID”)
因此,需要知道从哪里开始从以下JSON中提取所有节点。 ----- JSON Stream -----
{"areas":
[{"id":"1353331688024941487","name":"NGT","children":
[{"id":"1353331688024941488","name":"Other","children":
[
{"id":"1353331688024941489","name":"Independence Office","children":[]},
{"id":"1353331688024941490","name":"Lordstown Pipe Receipt Site (PRS)","children":[]},
{"id":"1353331688024941491","name":"Marion Pipe Receipt Site (PRS)","children":[]},
{"id":"1353331688024941492","name":"Marion Warehouse Facility","children":[]},
{"id":"1353331688024941493","name":"Port of Toledo Pipe Receipt Site (PRS)","children":[]},
{"id":"1353331688024941494","name":"Spread -1 - No facility","children":[]},
{"id":"1353331688024941495","name":"Spread -2 - Wadsworth Office","children":[]},
{"id":"1353331688024941496","name":"Spread -3 - Toledo Office","children":[]},
{"id":"1353331688024941497","name":"Spread -4 - Dundee Office","children":[]}
]
},
{"id":"1353331688024941498","name":"Spread-1","children":
[
{"id":"1353331688024941499","name":"Compressor Station 1","children":[]},
{"id":"1353331688024941500","name":"Field Inspectors 1","children":[]},
{"id":"1353331688024941501","name":"Meter Station 1","children":[]},
{"id":"1353331688024941502","name":"Meter Station 2","children":[]},
{"id":"1353331688024941503","name":"Meter Station 3","children":[]},
{"id":"1353331688024941504","name":"NDE Testing 1","children":[]},
{"id":"1353331688024941505","name":"Pipeline 1","children":[]},
{"id":"1353331688024941506","name":"Wareyard 1-1","children":[]}
]
},
{"id":"1353331688024941507","name":"Spread-2","children":
[
{"id":"1353331688024941508","name":"Compressor Station 2","children":[]},
{"id":"1353331688024941509","name":"Field Inspectors 2","children":[]},
{"id":"1353331688024941510","name":"Meter Station 5","children":[]},
{"id":"1353331688024941511","name":"NDE Testing 2","children":[]},
{"id":"1353331688024941512","name":"Pipeline 2","children":[]},
{"id":"1353331688024941513","name":"Wareyard 2-1","children":[]}
]
},
{"id":"1353331688024941514","name":"Spread-3","children":
[
{"id":"1353331688024941515","name":"Compressor Station 3","children":[]},
{"id":"1353331688024941516","name":"Compressor Station 4","children":[]},
{"id":"1353331688024941517","name":"Field Inspectors 3","children":[]},
{"id":"1353331688024941518","name":"Meter Station 6","children":[]},
{"id":"1353331688024941519","name":"NDE Testing 3","children":[]},
{"id":"1353331688024941520","name":"Pipeline 2","children":[]},
{"id":"1353331688024941521","name":"Wareyard 3-1","children":[]}
]
},
{"id":"1353331688024941522","name":"Spread-4","children":
[
{"id":"1353331688024941523","name":"Field Inspectors 4","children":[]},
{"id":"1353331688024941524","name":"Meter Station 4","children":[]},
{"id":"1353331688024941525","name":"NDE Testing 4","children":[]},
{"id":"1353331688024941526","name":"Pipeline 4","children":[]},
{"id":"1353331688024941527","name":"Wareyard 4-1","children":[]},
{"id":"1353331688024941528","name":"Wareyard 4-3","children":[]},
{"id":"1353331688024941529","name":"Wareyard 4-4","children":[]}
]
}
]
}
]
}
谢谢,感谢您的帮助。
答案 0 :(得分:1)
Visual Studio将创建您需要的类,或者您可以使用在线机器人。在VS中,使用剪贴板上的JSON,编辑 - > 选择性粘贴 - > 将JSON粘贴为类
两者都有点密集,因为他们不会认识到Child
类实际上是相同的,你可以使用相同的类。 事实上, Area
和Child
类也是相同的:
Public Class AreaContainer
Public Property areas As AreaItem()
End Class
Public Class AreaItem
Public Property id As String
Public Property name As String
Public Property children As AreaItem() ' no need for a Child1
End Class
反序列化:
Dim jstr = ...wherever you get it
Dim myareas = JsonConvert.DeserializeObject(Of AreaContainer)(jstr)
我不确定您的目标是什么,但这会打印NGT.Other
名儿童的姓名和他们的儿童的名字。
For n As Int32 = 0 To myareas.areas(0).children(0).children.Count - 1
Console.WriteLine("Name {0} => children: {1}",
myareas.areas(0).children(0).children(n).name,
myareas.areas(0).children(0).children(n).children.Count)
Next
名称独立办事处=>孩子:0
名称Lordstown Pipe Receipt Site(PRS)=>孩子:0
名称Marion Pipe Receipt Site(PRS)=>孩子:0
名称Marion Warehouse Facility =>孩子:0
名称托莱多管道接收站点(PRS)=>孩子:0
名称传播-1 - 没有设施=>孩子:0
名称差价-2 - Wadsworth Office =>孩子:0
名称差价-3 - 托莱多办公室=>孩子:0
名称传播-4 - 邓迪办公室=>孩子:0
如此处的其他帖子所示,您可以通过首先解析JSON来简化代码必须使用的结果对象,将areas
部分反序列化为数组。它省略了myAreas
变量以及AreaContainer
类,两者都没有用处。
答案 1 :(得分:0)
你需要再增加一个“AreaModel”类,其中包含一个属性“Areas”。 所以你的课程应如下所示。
public class AreaModel
{
public List<Area> Areas { get; set; }
}
public class Area
{
[JsonProperty(PropertyName = "id")]
public string Area_Name
{
get;
set;
}
[JsonProperty(PropertyName = "name")]
public string Area_ID {get;set;}
[JsonProperty(PropertyName= "children")]
public List<Area> Children
{
get;set;
}
}
然后您可以将JSON字符串反序列化如下。
var areaModel = JsonConvert.DeserializeObject<AreaModel>(areaJSON);
foreach (var area in areaModel.Areas)
{
DisplayAreas(area);
}
private static void DisplayAreas(Area area)
{
Console.WriteLine(area.Area_Name);
if(area.Children != null && area.Children.Count > 0)
{
foreach(var child in area.Children)
{
DisplayAreas(child);
}
}
}