我的JSON允許空值,並繼續
{
"name": "username",
"items": {
"myitem": {
"icon": "myitem.jpg",
"myitem2": {
"icon": "myitem2.jpg",
}
我的代碼
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Try
request = DirectCast(WebRequest.Create("http://myjson.com/json"), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp As String
rawresp = reader.ReadToEnd()
Dim jResults As JObject = JObject.Parse(rawresp)
name.Text = If(jResults2("name") Is Nothing, "", jResults2("name").ToString())
myitem.Text = If(jResults2("items")("myitem")("icon") Is Nothing, "", jResults2("items")("myitem")("icon").ToString())
myitem2.Text = If(jResults2("items")("myitem2")("icon") Is Nothing, "", jResults2("items")("myitem2")("icon").ToString())
'Catch ex As Exception
Catch ex As System.Net.WebException
MsgBox(ex.ToString)
Finally
If Not response Is Nothing Then response.Close()
End Try
IM有我的JSON一個問題,有時也可以是這樣的
{
"name": "username",
"items": {
"myitem2": {
"icon": "myitem2.jpg",
}
當我做如果myitem不在json字符串中,則檢查爲null,然後將名稱放在name.text中,但myitem2未處理或甚至放置在myitem2.Text中
任何幫助解決這一問題將是短期巨大的,如果myitem爲null,我仍然希望它繼續處理項目
JSON無效,您的JSON RootObject類是什麼樣的? – Dayan