无效的Json原语错误 - api调用结果

时间:2013-12-04 14:19:44

标签: json vb.net

我正在通过api调用获取以下数据 -

[{
    "generated_at": 1371490821,
    "status": "success",
    "station_id": 1,
    "station_name": "Hits Radio FM",
    "developer_id": 24,
    "count": 5,
    "refresh_url": "https://api.xxx.com/1/GET/nowplaying.json?api_key=[YOUR_API_KEY]&station_id=1&count=5",
    "next_results_url": "https://api.xxx.com/1/GET/nowplaying.json?api_key=[YOUR_API_KEY]&station_id=1since_id=85",
    "since_id": 85,
    "tracks": {
        "0": [{
            "id": 80,
            "artist": "Robbie Williams",
            "artist_id": 314,
            "title": "Candy"
            "likes": 215,
            "youtube_id": "gtOV7bp-gys",
            "coverart_url": "http://25.media.tumblr.com/tumblr_mdukt9rJoI1rj7xj3o1_500.jpg",
            "spotify_url": "https://play.spotify.com/track/6cskzfZWgrgd8hv74fHd9o",
            "itunes_url": "https://itunes.apple.com/nl/album/candy-single/id558817147",
            "playedtime": 1371490752,
            "likedByUser": 0
        }],
        "1": [{ /* the same layout as above */ }],
        "2": [{ /* the same layout as above */ }],
        "3": [{ /* the same layout as above */ }],
        "4": [{ /* the same layout as above */ }]
    }
}]

当我尝试使用JavaScriptSerializer反序列化此数据时,我收到错误消息“Invalid JSON primitive”。

任何人都可以帮我解决这个问题。

我正在使用以下用于保存数据的类。

Public Class ResponseTrackInfo
    Dim id As Integer
    Dim artist As String
    Dim artist_id As Integer
    Dim title As String
    Dim likes As Integer
    Dim youtube_id As String
    Dim coverart_url As String
    Dim spotify_url As String
    Dim itunes_url As String
    Dim playedtime As Integer
    Dim likedByUser As Integer
End Class

Public Class ResponseNowPlayingInfo
    Dim generated_at As Integer
    Dim status As String
    Dim station_id As Integer
    Dim station_name As String
    Dim developer_id As Integer
    Dim count As Integer
    Dim refresh_url As String
    Dim next_results_url As String
    Dim since_id As Integer
    Dim tracks As List(Of ResponseTrackInfo)

    Public Sub New()
        tracks = New List(Of ResponseTrackInfo)
    End Sub
End Class

和代码

Private Sub CallApiToGetLiveEvents()
        Dim apiResponse As WebResponse
        Dim request As HttpWebRequest
        Dim apiUrl As String = ConfigurationManager.AppSettings("My_URL")

        Dim apiKey As String = ConfigurationManager.AppSettings("My_ApiKey")
        Dim stationId As String = ConfigurationManager.AppSettings("My_StationId")
        apiUrl = String.Format(apiUrl, apiKey, stationId)


        Dim uri As New Uri(apiUrl)
        request = WebRequest.Create(uri)
        apiResponse = request.GetResponse()

        If Not (apiResponse Is Nothing) Then
            Dim data1(apiResponse.ContentLength) As Byte

            apiResponse.GetResponseStream().Read(data1, 0, data1.Length)

            Dim d = Encoding.ASCII.GetString(data1)

            Dim ser As New JavaScriptSerializer
            Dim decoded = ser.Deserialize(Of List(Of ResponseNowPlayingInfo))(d)

        End If
    End Sub

3 个答案:

答案 0 :(得分:1)

你可能在

之后错过了一个逗号
"title": "Candy"

在你的JSON对象中,这使得解析器真的很难将这个东西解析为JSON。 :)

答案 1 :(得分:0)

您的JSON无效,请尝试使用jsonlint.com来验证您的JSON

"title": "Candy"之后您缺少逗号

答案 2 :(得分:-3)

尝试将JSON字符串传递到文本框中,然后从textbox.text反序列化它。