使用VB.net和Newtonsoft.Json解析复杂的Json数据 [英] Parse Complex Json Data with VB.net and Newtonsoft.Json

查看:271
本文介绍了使用VB.net和Newtonsoft.Json解析复杂的Json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vb.net构建Windows应用程序,以获取一个Json文件,该文件将每天下载并解析,然后将值输入到SQL数据库中.我唯一要坚持的部分就是要解析Json数据以获得所需的值.

I'm building a windows application using vb.net to take a Json file that will be downloaded daily and parse it and enter the values into a SQL database. The only part I'm getting stuck on is being about to parse the Json data to get the values I need.

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"的值:

I'm trying to get the values for "last", "previousDay" and "previousYear" from:

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

data > mdc_cashprices_ > data > instruments > (where djShortName = "Gold-EnglehardFab" and djShortName = "Silver-EngelhrdFab" and djShortName = "Platinum-EngelhFab" and djShortName = "Palladium-EngelFab")

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

I'm also trying to get the value for "timestamp" from:

数据> mdc_cashprices_>数据>时间戳

data > mdc_cashprices_ > data > timestamp

在我当前的代码中,我只能进入第一个数据"层.

In my current code I can only get to the 1st "data" tier.

任何帮助将不胜感激.

推荐答案

使用类似 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 [])

因为它是一个数组,所以您可能要循环它

Since it's an array you would probably want to loop it

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

在这里还有一些格式问题>>> mdc_cashprices _ {\要求...<<< _和\不应该在那儿"

Btw there are some formatting issues here >>> mdc_cashprices_{\"req... <<< that _ and \ shouldn't be there"

这篇关于使用VB.net和Newtonsoft.Json解析复杂的Json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆