我在VB中反序列化这个JSON时遇到问题。我只能获取每个数组中的值,但无法获取表中字段名的键。您会注意到每个对象的大小可能会因反序列化而处理。
任何人都可以帮忙吗?
{
"MATRIX": [
{
"1": "FM",
"2": "4",
"3": "Floor type",
"4": "Standing water",
"5": "Property unsafe"
},
{
"1": "FM",
"2": "4",
"3": "Ceiling",
"4": "Mould spores",
"5": "Room/property still safe"
},
{
"1": "FL",
"2": "Basement",
"3": "1"
},
{
"1": "FL",
"2": "External",
"3": "7"
},
{
"1": "FL",
"2": "First Floor",
"3": "3"
},
{
"1": "FL",
"2": "Garden",
"3": "6"
},
{
"1": "FL",
"2": "Ground Floor",
"3": "2"
},
{
"1": "FL",
"2": "Second Floor",
"3": "4"
},
{
"1": "FL",
"2": "test 2",
"3": "9"
},
{
"1": "FL",
"2": "Third Floor",
"3": "5"
},
{
"1": "CA",
"2": "Bath",
"3": "Bathroom",
"4": "Bath"
},
{
"1": "CA",
"2": "Electrical",
"3": "Bathroom",
"4": "Bathroom Socket"
}
]
}
提前谢谢你,
d:)
嗨再次..我已经尝试了第二个建议中的代码,但无法同时获得每个项目的密钥和值。我得到了键,然后是我的字符串的每次迭代的值..
这就是我在做的事情。请你能指出我的错误..
Dim json As String = "{""items"":[{""Name"":""John"",""Age"":""20"",""Gender"":""Male""},{""Name2"":""Tom"",""Age2"":""25"",""Gender2"":""Male""},{""Name3"":""Sally"",""Age3"":""30"",""Gender3"":""Female""}]}"
Dim jss = New JavaScriptSerializer()
Dim data = jss.Deserialize(Of Object)(json)
For i = 0 To 2
For Each key As String In data("items")(i).Keys
MsgBox(key)
Next
'For Each item As Dictionary(Of String, Object) In data("items")
For Each val As String In data("items")(i).Values
MsgBox(val)
Next
Next
再次感谢你,
德里克。
答案 0 :(得分:0)
如果我正确理解了这个问题,您可以使用这些键来获取相应的值,使用以下内容:
For Each item In data("items")
Console.WriteLine("***item***")
For Each key In item.Keys
Console.WriteLine("{0}:{1}", key, item(key))
Next
Next
产生以下输出:
***item*** Name:John Age:20 Gender:Male ***item*** Name2:Tom Age2:25 Gender2:Male ***item*** Name3:Sally Age3:30 Gender3:Female