阅读Json数组vb.net

时间:2016-09-01 19:13:54

标签: json vb.net

我对此感到生气。 我对Json很新。 我需要从提供的json示例中读取数组(" items")。 我可以阅读所有其他对象,如" id"," title"," description" ...但不是项目数组。 使用Newtonsoft.Jason

代码(vb.net):>>

Dim json As String = File.ReadAllText("C:\Test\Json\test.json")
    Dim ser As JObject = JObject.Parse(json)
    Dim data As List(Of JToken) = ser.Children().ToList

    For Each item As JProperty In data
        item.CreateReader()
        Select item.Name
            Case "results"
                For Each comment As JObject In item.Values
                    txtConsole.Text = comment
                    Console.WriteLine(comment("id"))
                    Console.WriteLine(comment("title"))
                    Console.WriteLine(comment("description"))
                    Console.WriteLine(comment("tipe"))
                    Console.WriteLine(comment("author")("description"))
                    Console.WriteLine(comment("details")("conditions"))

                    'for each item in array
                        'Read the array of "products": here
                        'Console.WriteLine(comment("name")
                        'Console.WriteLine(comment("codeBar")
                    'next

                    Console.WriteLine(comment("details")("benefits"))
                    Console.WriteLine(comment("details")("price"))
                    Console.WriteLine(comment("details")("discount"))
                    Console.WriteLine(comment("details")("pays"))
                    Console.WriteLine(comment("datefrom"))
                    Console.WriteLine(comment("dateto"))
                Next

        End Select
    Next

Json文件>>

{
  "total": 1,
  "results": [
    {
      "id": 208,
      "title": "This is the title",
      "description": "This is the descripcion",
      "tipe": "This is type",
      "author": {
        "descripcion": "description of author"
      },
      "details": {
        "conditions": {
          "items": [
            {
              "quantity": 6,
              "products": [
                {
                  "name": "Product one",
                  "codeBar": "7891000100103"
                },
                {
                  "name": "Product two",
                  "codeBar": "7894900061604"
                },
                {
                  "name": "Product three",
                  "codeBar": "7894900010015"
                },
                {
                  "name": "Product four",
                  "codeBar": "7894900092011"
                }
              ]
            }
          ]
        },
        "benefits": null,
        "price": null,
        "discount": null,
        "pays": 5
      },
      "datefrom": "2015-08-06T00:00:00.000-0300",
      "dateto": "2016-12-31T23:59:59.000-0200"
    }
  ]
}

欲望控制台输出>>

208
This is the title
This is the descripcion
This is type

items
quantity: 6
products
          "name": "Product one",
          "codeBar": "7891000100103"

          "name": "Product two",
          "codeBar": "7894900061604"

          "name": "Product three",
          "codeBar": "7894900010015"

          "name": "Product four",
          "codeBar": "7894900092011"

5
06/08/2015 00:00:00
31/12/2016 22:59:59

请帮帮我...非常感谢你!!

2 个答案:

答案 0 :(得分:0)

这可能会指向正确的方向。我能够以这种方式访问​​产品阵列:

        ' Open the file using a stream reader.
    Dim sr As New StreamReader(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\json.txt")
    Dim line As String

    line = sr.ReadToEnd()
    line = "[" & line & "]"

    Dim jArray__1 = JArray.Parse(line)

    For Each item In jArray__1.SelectToken("[0].results.[0].details.conditions.items.[0].products")
        MessageBox.Show(item.ToString)
    Next

答案 1 :(得分:0)

非常感谢你们! 我终于这样做了......

For Each element In comment.SelectToken("details")("conditions")("items")(0)("products")
    Console.WriteLine(element("name"))
    Console.WriteLine(element("codeBar"))
Next