允许空闲并继续

时间:2014-02-24 17:11:40

标签: json vb.net string null

我的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放在name.text中,但myitem2不会被处理,甚至不会放在myitem2.Text中

如果myitem为null,任何帮助克服这个问题都会很好,我仍然希望它继续处理剩下的项目

1 个答案:

答案 0 :(得分:0)

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

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

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

如果这是正确的,那么这个JSON的RootObject看起来就像这样:

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; }
}