如何过滤json字符串的特定部分

时间:2017-08-30 17:40:39

标签: json vb.net

我已经在json上工作了大约2天而且它对我来说都是新的所以我正在学习,因为我去了这个字符串即时工作

{
    "elements": [{
        "id": "123",
        "hidden": false,
        "name": "testname",
        "sku": "1234",
        "price": 2499,
        "priceType": "FIXED",
        "defaultTaxRates": true,
        "cost": 1299,
        "isRevenue": true,
        "stockCount": 0,
        "itemStock": {
            "item": {
                "id": "123"
            },
            "stockCount": 3,
            "quantity": 3.0
        },
        "modifiedTime": 1504112254000
    }],
    "href": "http://api.server.com/items"
}

我使用的是Newtonsoft.Json库

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Net
Imports System.IO


Public Class Form1
    Dim token = ""
    Dim url = "https://api.Server.com/"
    Dim mid = ""

    Dim parm1 = ""
    Dim parm2 = ""
    Dim parm3 = ""
    Dim parm = parm1 & parm2 & parm3

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim jsonURL As String = url & mid & "/items?" & parm

        Dim reader As StreamReader
        Dim errorMsg As String = Nothing

        Try
            Dim request As HttpWebRequest = CType(WebRequest.Create(jsonURL), HttpWebRequest)
            request.Headers.Add("Authorization", "Bearer " + token)

            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            reader = New StreamReader(response.GetResponseStream())
            Dim jsonStr As String = reader.ReadToEnd()
            Dim JsonObject As JObject = JObject.Parse(jsonStr)
            Dim jsonArray As JArray = JArray.Parse(JsonObject.SelectToken("elements").ToString)
            For Each items As JObject In jsonArray
                Dim product As item = JsonConvert.DeserializeObject(Of item)(items.ToString)

                idTxtbox.Text = product.id
                hiddenTxtbox.Text = product.hidden
                nameTxtbox.Text = product.name
                skuTxtbox.Text = product.sku
                priceTxtbox.Text = product.price
                priceTypeTxtbox.Text = product.priceType
                defaultTaxRatesTxtbox.Text = product.defaultTaxRates
                CostTxtbox.Text = product.cost
                isRevenueTxtbox.Text = product.isRevenue
                stockCountTxtbox.Text = product.stockCount

                For Each itema In product.itemStock
                    ListBox1.Items.Add(itema("itemStock"))
                Next

                modifiedTimeTxtbox.Text = product.modifiedTime

                MsgBox("")

            Next
        Catch ex As WebException
            errorMsg = "Download failed. The response from the server was: " +
                   CType(ex.Response, HttpWebResponse).StatusDescription
            Console.WriteLine("Error: " + errorMsg)
        End Try

    End Sub

End Class

Class item
    Public id As String
    Public hidden As String
    Public name As String
    Public sku As String
    Public price As String
    Public priceType As String
    Public defaultTaxRates As String
    Public cost As String
    Public isRevenue As String
    Public stockCount As String
    'Public itemStock As Dictionary(Of String, String) = New Dictionary(Of String, String)
    'Public itemStock As List(Of String) = New List(Of String)

    Public Property itemStock() As IList(Of String)
        Get
            Return m_itemStock
        End Get
        Set(ByVal value As IList(Of String))
            m_itemStock = value
        End Set
    End Property

    Private m_itemStock As IList(Of String)
    Public modifiedTime As String


End Class

我可以获得第一组数据的所有值,但似乎无法显示" stockCount":3以显示我怎么能说出这个?

抱歉,如果我不是非常清楚,如果您需要更多信息,我会尽力提供

我已经更新了我到目前为止的代码我已经让JsonConvert.DeserializeObject工作但仍然无法获得

的值
"itemStock": {
                "item": {
                    "id": "123"
                },
                "stockCount": 3,
                "quantity": 3.0
            },

填充listbox1.items

我收到此错误

  

无法反序列化当前的JSON对象(例如{" name":" value"})   into type' System.Collections.Generic.IList`1 [System.String]'因为   该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。要修复此错误,请将JSON更改为JSON数组   (例如[1,2,3])或更改反序列化类型以使其正常   .NET类型(例如,不是整数的基本类型,不是集合   类似于数组或列表,可以是

0 个答案:

没有答案