使用VB.net和Newtonsoft.Json解析复杂的Json数据

时间:2019-07-10 12:06:50

标签: json vb.net winforms json.net

我正在使用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_>数据>时间戳

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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 ... <<< _和\不应该在那儿”