反序列化API响应

时间:2014-07-14 22:11:32

标签: .net json vb.net serialization

我在这里搜索了stackoverflow,并在Google上搜索了我遇到的问题。 我一直试图从这个网址反序列化json响应http://pastebin.com/d9wZpiYV

http://ponyvillelive.com/api/nowplaying/index/id/22

我可以使用以下代码获得json reulst:

Dim request = TryCast(System.Net.WebRequest.Create("http://ponyvillelive.apiary-proxy.com/api/nowplaying/index/id/{id}"), System.Net.HttpWebRequest)
request.Method = "GET"
request.ContentLength = 0
Dim responseContent As String
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
Using reader = New System.IO.StreamReader(response.GetResponseStream())
responseContent = reader.ReadToEnd()
End Using
End Using

我需要从结果中提取的所有信息是: “站” “ID” “名称” “类型” “图片网址” “stream_url” “艺术家” “标题”

我尝试的主要示例是Deserializing JSON String to VB.net Object,但是,我仍然是vb.net的新手。 如果有人能帮助我,我会非常感激。

1 个答案:

答案 0 :(得分:0)

您需要创建一个与Json对象相对应的模型JsonClass才能正确反序列化。您需要的字段位于json响应中的嵌套json对象中。基于您从URL获得的Json响应,我创建了一个映射到响应的类。仅供参考,以下是json回复

 {
 "status": "success",
 "result": {
  "station": {
   "id": 22,
   "name": "Wonderbolt Radio",
   "shortcode": "wonderbolt_radio",
   "genre": "Rock Music",
   "category": "audio",
   "type": "shoutcast2",
   "image_url": "http://ponyvillelive.com/static/stations/20131020_222721_679_image_url_1.png",
   "web_url": "http://wonderboltradio.com/",
   "stream_url": "http://pvlradio.bravelyblue.com:8062/stream?sid=1;stream.nsv",
   "twitter_url": "",
   "irc": "#wonderboltradio"
 },
 "listeners": {
   "current": 0,
   "unique": 0,
   "total": 0
 },
 "current_song": {
   "id": "633ab2e87847a5dd77a385f83022aef9",
   "text": "Sicknessia - Magic",
   "artist": "Sicknessia",
   "title": "Magic",
   "score": 100,
   "sh_id": 1364278,
  "vote_urls": {
    "like": "http://ponyvillelive.com/api/song/like/sh_id/1364278",
    "dislike": "http://ponyvillelive.com/api/song/dislike/sh_id/1364278",
    "clearvote": "http://ponyvillelive.com/api/song/clearvote/sh_id/1364278"
  },
  "external": []
},

以下代码应该适合您。

Imports System.Net
Imports System.Runtime.Serialization

Public Class JsonParser
Public Shared Sub ParseJson()
    Const url As String = "http://ponyvillelive.apiary-proxy.com/api/nowplaying/index/id/22"
    Try
        Dim jsResponse = LoadJson(Of JsonClass)(url)

        Dim id = jsResponse.result.station.id
        Dim name = jsResponse.result.station.name
        Dim genre = jsResponse.result.station.genre
        Dim imageUrl = jsResponse.result.station.image_url
        Dim streamUrl = jsResponse.result.station.stream_url
        Dim artist = jsResponse.result.current_song.artist
        Dim title = jsResponse.result.current_song.title
    Catch e As Exception
        Throw New Exception("Could not call webservice")
    End Try
End Sub

Private Shared Function LoadJson(Of T)(uri As String) As T
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(uri), HttpWebRequest)
    request.Method = "GET"
    request.ContentType = "application/json; charset=utf-8"
    request.ReadWriteTimeout = 30000

    Using httpResponse = request.GetResponse()
        Using stream = httpResponse.GetResponseStream()
            Dim jsonSerializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(T))
            Dim obj As T = DirectCast(jsonSerializer.ReadObject(stream), T)

            Return obj
        End Using
    End Using
 End Function
 End Class

Public Class JsonClass
 Public Property result() As Result
    Get
        Return m_result
    End Get
    Set(value As Result)
        m_result = Value
    End Set
 End Property
Private m_result As Result
End Class

Public Class Result
 Public Property station() As Station
    Get
        Return m_station
    End Get
    Set(value As Station)
        m_station = Value
    End Set
 End Property
 Private m_station As Station

Public Property current_song() As CurrentSong
    Get
        Return m_current_song
    End Get
    Set(value As CurrentSong)
        m_current_song = value
    End Set
End Property
  Private m_current_song As CurrentSong
End Class

Public Class Station
Public Property id() As Integer
    Get
        Return m_id
    End Get
    Set(value As Integer)
        m_id = Value
    End Set
 End Property
Private m_id As Integer
Public Property name() As String
    Get
        Return m_name
    End Get
    Set(value As String)
        m_name = Value
    End Set
End Property
Private m_name As String
Public Property genre() As String
    Get
        Return m_genre
    End Get
    Set(value As String)
        m_genre = Value
    End Set
End Property
Private m_genre As String
Public Property image_url() As String
    Get
        Return m_image_url
    End Get
    Set(value As String)
        m_image_url = Value
    End Set
End Property
Private m_image_url As String
Public Property stream_url() As String
    Get
        Return m_stream_url
    End Get
    Set(value As String)
        m_stream_url = Value
    End Set
 End Property
 Private m_stream_url As String
End Class

Public Class CurrentSong
 Public Property artist() As String
    Get
        Return m_artist
    End Get
    Set(value As String)
        m_artist = Value
    End Set
 End Property
 Private m_artist As String
 Public Property title() As String
    Get
        Return m_title
    End Get
    Set(value As String)
        m_title = Value
    End Set
 End Property
 Private m_title As String
End Class