您如何解析此JSON数据?

时间:2019-03-08 06:49:43

标签: json vb.net

我有此JSON数据要解析,但不确定如何处理。
这是JSON

{
   "input":{
      "lat":32.761125,
      "lon":-96.791339
   },
   "results":[
      {
         "block_fips":"481130204001105",
         "bbox":[
            -96.79587,
            32.753273,
            -96.787714,
            32.76218
         ],
         "county_fips":"48113",
         "county_name":"Dallas",
         "state_fips":"48",
         "state_code":"TX",
         "state_name":"Texas",
         "block_pop_2015":2,
         "amt":"AMT004",
         "bea":"BEA127",
         "bta":"BTA101",
         "cma":"CMA009",
         "eag":"EAG005",
         "ivm":"IVM009",
         "mea":"MEA032",
         "mta":"MTA007",
         "pea":"PEA008",
         "rea":"REA005",
         "rpc":"RPC004",
         "vpc":"VPC004"
      }
   ]
}

我使用Visual Studio工具Edit -> Paste Special -> Paste as JSON classes在类中转换了JSON,它为我提供了这样的类结构:

Public Class Rootobject
    Public Property input As Input
    Public Property results() As Result
End Class

Public Class Input
    Public Property lat As Single
    Public Property lon As Single
End Class

Public Class Result
    Public Property block_fips As String
    Public Property bbox() As Single
    Public Property county_fips As String
    Public Property county_name As String
    Public Property state_fips As String
    Public Property state_code As String
    Public Property state_name As String
    Public Property block_pop_2015 As Integer
    Public Property amt As String
    Public Property bea As String
    Public Property bta As String
    Public Property cma As String
    Public Property eag As String
    Public Property ivm As String
    Public Property mea As String
    Public Property mta As String
    Public Property pea As String
    Public Property rea As String
    Public Property rpc As String
    Public Property vpc As String
End Class

所以我尝试解析数据的是:

Dim MyJSON As String = JsonAbove
Dim jss As New JavaScriptSerializer()
Dim dict = jss.Deserialize(Of Result)(rawresp)
MsgBox(dict.state_name)

我得到的只是一个没有结果的空白MessageBox。
我在做错什么吗?

2 个答案:

答案 0 :(得分:1)

在类中使用对象列表而不是数组。我做了两个更改,一个是在根对象中获取结果,另一个在bbox的结果类中

Public Class Rootobject
    Public Property input As Input
    Public Property results As List(Of Result)
End Class

Public Class Input
    Public Property lat As Single
    Public Property lon As Single
End Class

Public Class Result
    Public Property block_fips As String
    Public Property bbox As List(Of Single)
    Public Property county_fips As String
    Public Property county_name As String
    Public Property state_fips As String
    Public Property state_code As String
    Public Property state_name As String
    Public Property block_pop_2015 As Integer
    Public Property amt As String
    Public Property bea As String
    Public Property bta As String
    Public Property cma As String
    Public Property eag As String
    Public Property ivm As String
    Public Property mea As String
    Public Property mta As String
    Public Property pea As String
    Public Property rea As String
    Public Property rpc As String
    Public Property vpc As String
End Class

然后访问属性

  Dim jss As New JavaScriptSerializer()
  Dim dict = jss.Deserialize(Of Rootobject)(MyJSON)
  MsgBox(dict.results.FirstOrDefault().state_name)

答案 1 :(得分:1)

相同,使用Newtonsoft.Json命名空间。

已使用<JsonProperty>属性为类属性分配了新名称。
此外, Results 属性已修改为返回List(Of Result)

反序列化非常简单明了:
您可以使用Visual Studio NuGet Package Manager安装Newtonsoft.Json

Imports Newtonsoft.Json

Dim latlonResults As RootObject = JsonConvert.DeserializeObject(Of RootObject)(JSON)
Dim state As String = latlonResults.Results(0).StateName

或在反序列化时直接访问属性:

Dim state As String = JsonConvert.DeserializeObject(Of RootObject)(JSON).Results(0).StateName

重构类:

Public Class RootObject 
    <JsonProperty("input")>
    Public Property Input() As Input

    <JsonProperty("results")>
    Public Property Results() As List(Of Result)
End Class

Public Class Input
    <JsonProperty("lat")>
    Public Property Lat() As Double

    <JsonProperty("lon")>
    Public Property Lon() As Double
End Class

Public Class Result
    <JsonProperty("block_fips")>
    Public Property BlockFips() As String

    <JsonProperty("bbox")>
    Public Property Bbox() As List(Of Double)

    <JsonProperty("county_fips")>
    Public Property CountyFips() As Long

    <JsonProperty("county_name")>
    Public Property CountyName() As String

    <JsonProperty("state_fips")>
    Public Property StateFips() As Long

    <JsonProperty("state_code")>
    Public Property StateCode() As String

    <JsonProperty("state_name")>
    Public Property StateName() As String

    <JsonProperty("block_pop_2015")>
    Public Property BlockPop2015() As Long

    <JsonProperty("amt")>
    Public Property Amt() As String

    <JsonProperty("bea")>
    Public Property Bea() As String

    <JsonProperty("bta")>
    Public Property Bta() As String

    <JsonProperty("cma")>
    Public Property Cma() As String

    <JsonProperty("eag")>
    Public Property Eag() As String

    <JsonProperty("ivm")>
    Public Property Ivm() As String

    <JsonProperty("mea")>
    Public Property Mea() As String

    <JsonProperty("mta")>
    Public Property Mta() As String

    <JsonProperty("pea")>
    Public Property Pea() As String

    <JsonProperty("rea")>
    Public Property Rea() As String

    <JsonProperty("rpc")>
    Public Property Rpc() As String

    <JsonProperty("vpc")>
    Public Property Vpc() As String
End Class