我的问题如下:
我创建的Public子目前看起来像这样:
Public Sub eniroContacts()
Dim phone As String = newCustPhone1.text
Dim url As String = "http://bedrift.telefonkatalogen.no/tk/search.php?qry=" + phone + "&from=1&to=27&format=json&username=user&password=pw"
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
Dim o As JObject = JObject.Parse(reader.ReadToEnd)
lblMsg.Text = o.ToString
reader.Close()
response.Close()
End Sub
到目前为止很好,我的lblMsg.Text显示了我想从中获取数据的json字符串(为了清晰起见,格式化):
{
"qry": "92020945",
"result": {
"hitLinesBeforeFilter": 1,
"userID": 299228,
"1": {
"listing": {
"table": "listing",
"id": "3647950",
"duplicates": [{
"table": "listing",
"id": "3647950:0",
"idlinje": "E19CMOD",
"tlfnr": "99887766",
"etternavn": "Omnes",
"fornavn": "Martin",
"veinavn": "Highway",
"husnr": "20",
"postnr": "0601",
"virkkode": "P",
"apparattype": "M",
"telco": "TM",
"kilde": "E",
"bkdata": "M",
"prioritet": "0",
"fodselsdato": "1976-07-03",
"kommunenr": "301",
"poststed": "Oslo",
"kommune": "Oslo",
"fylke": "Oslo",
"landsdel": "Ø"
}]
}
},
"dummy": null
}
}
我现在要做的是实际获取字符串中的一些值并将它们放入我的一些文本框和标签中,但是我不知道如何做到这一点。例如,我想要值" fornavn"," tlfnr"和" fylke"。在本例中,值为Martin,99887766和Oslo,应放在textbox1,2和3中。
另外我想补充说,有些字段可能不会出现在某些搜索中,例如名字(" fornavn"在我的例子中)。当这不在那里,我尝试写作:
Dim fornavn = o("result")("1")("listing")("duplicates")(0)("fornavn")
它崩溃了。我可以采取哪些措施来防止这种情况发生?
非常感谢任何线索和提示! :)
答案 0 :(得分:0)
最简单的方法是直接使用属性名称和数组索引(来自json.net docs)。像这样:
Dim fornavn = o("result")("1")("listing")("duplicates")(0)("fornavn")
看起来响应中的结果和重复数量会在不同的响应之间发生变化,因此您可能无法对其进行硬编码(例如,您可能需要首先检查结果是否至少包含一个项目,还有重复项至少有一个项目)
另一种方法是将字符串反序列化为强类型的.Net对象。您将创建镜像json结构的.Net类,并告诉json.net将json字符串转换为这些类的.Net对象实例。请参阅json.net docs。这需要更多的工作,因为您需要创建.Net类,您需要了解反序列化过程。如果您想探索此选项,请查看this和this other个问题。
给定json结构,在json对象的result
属性中手动获取类似“1”的属性可能更容易。
然后,您可以选择手动继续在对象中钻取并迭代重复项,应用您需要的任何逻辑,或反序列化为.Net对象。
以下代码将获取result
属性,使用键1,2等对其所有属性进行迭代,并使用两种方法在每个副本中打印名称:
Sub Main()
Dim JsonStr = "... the json string ..."
Dim o As JObject = JObject.Parse(JsonStr)
Dim results = o("result")
For Each resultProperty In results.Value(Of JObject)()
'Only get properties like "1" inside the root "result" property
If Not Integer.TryParse(resultProperty.Key, Nothing) Then Continue For
'Approach 1: Manually Iterate over the duplicates array inside each result
Dim duplicatesArray = resultProperty.Value("listing")("duplicates").Value(Of JArray)()
For Each duplicate In duplicatesArray
'Make sure there is a fornavn property
If duplicate("fornavn") Is Nothing Then Continue For
Console.WriteLine(duplicate("fornavn"))
Next
'Approach 2: Deserialize the listing into a .Net object
Dim serializer As JsonSerializer = New JsonSerializer()
Dim resultObject As Result = JsonConvert.DeserializeObject(Of Result)(resultProperty.Value.ToString())
For Each duplicateObject In resultObject.listing.duplicates
Console.WriteLine(duplicateObject.fornavn)
Next
Next
Console.ReadKey()
End Sub
Class Result
Property listing As Listing
End Class
Class Listing
Property table As String
Property id As String
Property duplicates As Duplicate()
End Class
Class Duplicate
Property table As String
Property id As String
Property idlinje As String
Property fornavn As String
'Continues with all the other properties...
End Class
请注意我只是快速编写了这段代码,因此如果json响应中并不总是存在某些属性,则可能需要调整它。