简体   繁体   中英

How do you parse this JSON data?

I have this JSON data that I would like to parse but I'm not sure how to go about it.
Here's the 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"
      }
   ]
}

I used the Visual Studio tool, Edit -> Paste Special -> Paste as JSON classes , to convert this JSON in classes and it gave me this class structure:

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

So then what I did to try and parse the data is:

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

All I get is a blank MessageBox with no results.
Am I doing something wrong?

Use List Of Objects instead of array in your class. I have done two changes One in the rootobject for results and other in the result class for 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

Then access the properties

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

Same, using the Newtonsoft.Json namespace.

The classes properties have being assigned new names, using a <JsonProperty> attribute.
Also, the Results property is modified to return a List(Of Result) .

The deserialization is pretty simple and straightforward:
You can use the Visual Studio NuGet Package Manager to install Newtonsoft.Json .

Imports Newtonsoft.Json

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

or access a property directly while deserializing:

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

Refactored classes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM