允许空值并继续



My 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

我的 JSON 有问题,因为有时它看起来像这样

{
"name": "username",
   "items": {
    "myitem2": {
      "icon": "myitem2.jpg",
}

当我检查 null 时,如果 myItem 不在 JSON 字符串中,则名称被放置在 Name.text 中,但 myitem2 不会被处理,甚至不会放在 myitem2 中。发短信

简而言之,如果myitem为空,那么解决此问题的任何帮助都将很棒,我仍然希望它继续处理其余项目

示例中的 JSON 无效,您可以访问此站点来验证您的 JSON:http://jsonlint.com/

我认为您想要的内容如下所示:

{
    "name": "username",
    "items": {
        "myitem": {
            "icon": "myitem.jpg"
        },
        "myitem2": {
            "icon": "myitem2.jpg"
        }
    }
}

如果这是正确的,那么此 JSON 的根对象将如下所示:

public class Myitem
{
    public string icon { get; set; }
}
public class Myitem2
{
    public string icon { get; set; }
}
public class Items
{
    public Myitem myitem { get; set; }
    public Myitem2 myitem2 { get; set; }
}
public class RootObject
{
    public string name { get; set; }
    public Items items { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新