【问题标题】:Parse Complex Json Data with VB.net and Newtonsoft.Json使用 VB.net 和 Newtonsoft.Json 解析复杂的 Json 数据
【发布时间】:2019-07-10 12:06:50
【问题描述】:

我正在使用 vb.net 构建一个 Windows 应用程序来获取一个 Json 文件,该文件将每天下载并解析它并将值输入到 SQL 数据库中。我唯一卡住的部分是即将解析 Json 数据以获取我需要的值。

Public Sub json_parse(ByVal result_json As String)
        Try
            Dim json As String = result_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 Case item.Name
                    Case "data"
                        For Each msg As JObject In item.Values
                            MsgBox(msg.ToString)
                        Next
                End Select
            Next
        Catch __unusedException1__ As Exception
            'MsgBox(__unusedException1__.ToString)
        End Try
    End Sub

{
  "data": {
    "wsj_mdstrip_na,us": {
      "set": 1562756496071,
      "ttl": 300000,
      "data": {
        "id": "na,us",
        "type": "wsj_mdstrip",
        "data": [
          {
            "timeZone": "CDT",
            "localTimeZoneTimestamp": "5:51 AM CDT 07/10/19",
            "localTimestamp": "5:51 AM 07/10/19",
            "etTimestamp": "6:51 AM ET 07/10/19",
            "mdTimestamp": "2019-07-10T06:51:35-04:00",
            "timestamp": "2019-07-10T05:51:35.365",
            "chartingSymbol": "Future/US/XCBT/YM00",
            "updatedTimestamp": "2019-07-10T07:01:36-04:00"
          },
          {
            "deltaBarPosNeg": "deltaBar-neg",
            "etTimestamp": "6:51 AM ET 07/10/19",
            "chartingSymbol": "Future/US/XCME/ES00",
            "pastCloses": [
              {
                "range": "P1Y",
                "price": 2775.75
              },
              {
                "range": "P3Y",
                "price": 2145.75
              }
            ]
          }
        ]
      }
    },
    "wsj_nav_na,us": {
      "set": 1562754843231,
      "ttl": 1800000,
      "isStale": false
    },
    "mdc_cashprices_": {
      "set": 1562756402351,
      "ttl": 90000,
      "data": {
        "id": "{\"requestedCommodities\":\"all\",\"groupIntoMapBy\":\"type\",\"groupIntoSetsBy\":\"subType\"}",
        "type": "mdc_cashprices",
        "data": {
          "instruments": [
            {
              "code": null,
              "djShortName": "Gold-EnglehardFab",
              "instrumentID": "511498",
              "name": "Engelhard fabricated products",
              "subType": "Gold, per troy oz",
              "type": "Precious metals",
              "ask": "-",
              "bid": "-",
              "last": "1500.72",
              "previousDay": "1509.33",
              "previousYear": "1351.58"
            },
            {
              "code": null,
              "djShortName": "Silver-EngelhrdFab",
              "instrumentID": "511558",
              "name": "Engelhard fabricated products",
              "subType": "Silver, troy oz.",
              "type": "Precious metals",
              "ask": "-",
              "bid": "-",
              "last": "18.0600",
              "previousDay": "18.0960",
              "previousYear": "19.3200"
            },
            {
              "code": null,
              "djShortName": "SilverHandy&HarmB",
              "instrumentID": "511560",
              "name": "Handy & Harman base price",
              "subType": "Silver, troy oz.",
              "type": "Precious metals",
              "ask": "-",
              "bid": "-",
              "last": "15.1070",
              "previousDay": "15.0420",
              "previousYear": "16.1000"
            },
            {
              "code": null,
              "djShortName": "Platinum-EngelhFab",
              "instrumentID": "511554",
              "name": "Platinum, Engelhard fabricated products",
              "subType": "Other precious metals",
              "type": "Precious metals",
              "ask": "-",
              "bid": "-",
              "last": "912.0",
              "previousDay": "914.0",
              "previousYear": "946.0"
            },
            {
              "code": null,
              "djShortName": "Palladium-EngelInd",
              "instrumentID": "511553",
              "name": "Palladium, Engelhard industrial bullion",
              "subType": "Other precious metals",
              "type": "Precious metals",
              "ask": "-",
              "bid": "-",
              "last": "1561.0",
              "previousDay": "1573.0",
              "previousYear": "955.0"
            },
            {
              "code": null,
              "djShortName": "Palladium-EngelFab",
              "instrumentID": "511552",
              "name": "Palladium, Engelhard fabricated products",
              "subType": "Other precious metals",
              "type": "Precious metals",
              "ask": "-",
              "bid": "-",
              "last": "1661.0",
              "previousDay": "1673.0",
              "previousYear": "1055.0"
            }
          ],
          "timestamp": "7/09/19"
        }
      }
    },
    "consumer_corphat_corphat": {
      "set": 1562756495973,
      "ttl": 90000,
      "data": {
        "id": "corphat",
        "type": "consumer_corphat",
        "data": [
          {
            "ncLinks": [
              {
                "title": "Big Decisions",
                "url": "http://www.bigdecisions.com/",
                "nofollow": "false"
              },
              {
                "title": "Business Spectator",
                "url": "https://www.businessspectator.com.au/",
                "nofollow": "false"
              }
            ]
          },
          {
            "djLinks": [
              {
                "title": "Barron's",
                "url": "https://www.barrons.com",
                "nofollow": "false"
              },
              {
                "title": "BigCharts",
                "url": "http://bigcharts.marketwatch.com",
                "nofollow": "false"
              }
            ]
          }
        ]
      },
      "isStale": false
    }
  },
  "currentState": {
    "data": [
      "0.0.3.0.0",
      "0.0.3.1.0.0.2"
    ],
    "components": {
      "code___decoratedComponent___275181c7-8620-4df3-a008-d0cd9937db22___WSJTheme---WSJBase---WSJForms---WSJTables": {
        "id": "275181c7-8620-4df3-a008-d0cd9937db22",
        "decorators": [
          "WSJTheme",
          "WSJBase",
          "WSJForms",
          "WSJTables"
        ]
      },
      "code___decoratedComponent___c8882c9c-15d3-4d1f-9b0e-81b6f321365d___WSJTheme---WSJBase---WSJForms---WSJTables": {
        "id": "c8882c9c-15d3-4d1f-9b0e-81b6f321365d",
        "decorators": [
          "WSJTheme",
          "WSJBase",
          "WSJForms",
          "WSJTables"
        ]
      }
    }
  }
}

我正在尝试从以下位置获取“last”、“previousDay”和“previousYear”的值:

数据 > mdc_cashprices_ > 数据 > 工具 >(其中 djShortName = "Gold-EnglehardFab" 和 djShortName = "Silver-EngelhrdFab" 和 djShortName = "Platinum-EngelhFab" 和 djShortName = "Palladium-EngelFab")

我也在尝试从以下位置获取“时间戳”的值:

数据 > mdc_cashprices_ > 数据 > 时间戳

在我当前的代码中,我只能到达第一个“数据”层。

任何帮助将不胜感激。

【问题讨论】:

    标签: json vb.net winforms json.net


    【解决方案1】:

    使用https://jsonformatter.curiousconcept.com/ 之类的内容可以更轻松地查看它。

    那么简单

    Dim Something as JObject = JObject.Parse(<your JSON>)
    

    然后得到你想要的

    Something1 = Something("data")("mdc_cashprices")("data")("data")("instruments")(0)("last") 'basically you just traverse the levels, you use the id/text for regular stuff (inside {}) and numbers for arrays (inside [])
    

    因为它是一个数组,你可能想要循环它

    For Each item In Something("data")("mdc_cashprices")("data")("data")("instruments")
            Something2 = item("last")
    Next
    

    顺便说一句,这里有一些格式问题 >>> mdc_cashprices_{\"req...

    【讨论】:

    • 感谢您对第一部分的帮助。也许你也可以帮助我完成第二部分。我正在尝试获取以下值: data > mdc_cashprices_ > data > data > timestamp 通过使用此代码: Dim ser As JObject = JObject.Parse(result_json) For Each item In ser("data")("mdc_cashprices_") ("data")("data") Dim timestamp As String = item("timestamp") gatherData = timestamp Next 但我不断收到此错误:“无法访问 Newtonsoft.Json.Linq.JProperty 上的子值。”
    • 在timestap设置一个断点,看看item是不是什么东西。如果是检查它,看看时间戳在哪里,也请用更正的那个来编辑问题中的 json。
    • “item”包含:“timestamp”:“7/09/19”,所以看起来我很高兴。感谢您的所有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2017-12-05
    相关资源
    最近更新 更多