无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.IList

时间:2015-10-27 07:01:18

标签: c# json vb.net serialization json.net

我有以下JSON

{
"response": {
  "feedMessageResponse": {
     "count": 1,
     "feed": {
        "id": "0nOCG6C0TBCMWdT0YPQYm4n8zxxxxgDQ",
        "name": "Nav1",
        "description": "Nav 1",
        "status": "ACTIVE",
        "usage": 0,
        "daysRange": 7,
        "detailedMessageShown": true
     },
     "totalCount": 1,
     "activityCount": 0,
     "messages": {
        "message": {
           "@clientUnixTime": "0",
           "id": 464064302,
           "messengerId": "0-8061742",
           "messengerName": "Navigator1",
           "unixTime": 1445904943,
           "messageType": "OK",
           "latitude": -39.1234,
           "longitude": 140.1234,
           "modelId": "SPOT2",
           "showCustomMsg": "Y",
           "dateTime": "2015-10-27T00:15:43+0000",
           "batteryState": "GOOD",
           "hidden": 0,
           "messageContent": "We have reached this place safely."
        }
     }
   }
 }
}

有大量属性,但只有1个响应.feedMessageResponse.messages.message我得到一个反序列化错误说明:

  

无法反序列化当前的JSON对象(例如{“name”:“value”})   进入类型   'System.Collections.Generic.IList`1 [ITBeyond.Mapping.Tracking + SpotMessenger +消息]'   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。要修复此错误,请将JSON更改为JSON数组   (例如[1,2,3])或更改反序列化类型以使其正常   .NET类型(例如,不是整数的基本类型,不是集合   类似于数组或List的类型,可以从JSON反序列化   宾语。 JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。路径   'response.feedMessageResponse.messages.message。@ clientUnixTime',行   1,位置287.

如果有多条消息,则可以正常工作。围绕此错误的两个属性定义为:

Public Class Messages
            <JsonProperty("message")> _
            Public Property Message() As IList(Of Message)
                Get
                    Return m_Message
                End Get
                Set(value As IList(Of Message))
                    m_Message = value
                End Set
            End Property
            Private m_Message As IList(Of Message)
        End Class

        Public Class Message
            <JsonProperty("@clientUnixTime")> _
            Public Property ClientUnixTime() As String
                Get
                    Return m_ClientUnixTime
                End Get
                Set(value As String)
                    m_ClientUnixTime = value
                End Set
            End Property
         Private m_ClientUnixTime As String

我花了很多时间寻找这个,并想知道@是否有问题。我也尝试过更改为Message数组,但遇到了类似的错误。任何想法。

如果有多个这样的话 - 它会转到一个消息数组:

{"response":{"feedMessageResponse":{"count":50,"feed":{"id":"065QbDkxboXg22n86GUPEQeuauruGOYBi","name":"SPOT XML","description":"SPOT XML","status":"ACTIVE","usage":0,"daysRange":7,"detailedMessageShown":true},"totalCount":182,"activityCount":0,"messages":{"message":[{"@clientUnixTime":"0","id":464123101,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445922060,"messageType":"UNLIMITED-TRACK","latitude":-35.19292,"longitude":149.03433,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T05:01:00+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464122174,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445921763,"messageType":"UNLIMITED-TRACK","latitude":-35.19292,"longitude":149.0343,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T04:56:03+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464121461,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445921495,"messageType":"UNLIMITED-TRACK","latitude":-35.19289,"longitude":149.0343,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T04:51:35+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464120694,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445921168,"messageType":"UNLIMITED-TRACK","latitude":-35.19308,"longitude":149.03423,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T04:46:08+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464115761,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445919383,"messageType":"UNLIMITED-TRACK","latitude":-35.19321,"longitude":149.03415,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T04:16:23+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464115023,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445919086,"messageType":"UNLIMITED-TRACK","latitude":-35.19305,"longitude":149.03415,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T04:11:26+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464114294,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445918788,"messageType":"UNLIMITED-TRACK","latitude":-35.19316,"longitude":149.03424,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T04:06:28+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464113424,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445918490,"messageType":"UNLIMITED-TRACK","latitude":-35.1931,"longitude":149.03429,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T04:01:30+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464112469,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445918192,"messageType":"UNLIMITED-TRACK","latitude":-35.19315,"longitude":149.03419,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:56:32+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464111792,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445917972,"messageType":"UNLIMITED-TRACK","latitude":-35.19928,"longitude":149.0296,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:52:52+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464110787,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445917597,"messageType":"UNLIMITED-TRACK","latitude":-35.24193,"longitude":149.02847,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:46:37+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464109932,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445917299,"messageType":"UNLIMITED-TRACK","latitude":-35.27621,"longitude":149.0704,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:41:39+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464109006,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445917001,"messageType":"UNLIMITED-TRACK","latitude":-35.33484,"longitude":149.06393,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:36:41+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464108035,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445916704,"messageType":"UNLIMITED-TRACK","latitude":-35.38843,"longitude":149.06499,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:31:44+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464107372,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445916504,"messageType":"UNLIMITED-TRACK","latitude":-35.41505,"longitude":149.07426,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:28:24+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464106371,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445916108,"messageType":"UNLIMITED-TRACK","latitude":-35.43842,"longitude":149.12648,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:21:48+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464105481,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445915810,"messageType":"UNLIMITED-TRACK","latitude":-35.50751,"longitude":149.15002,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:16:50+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464105500,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445915510,"messageType":"UNLIMITED-TRACK","latitude":-35.57841,"longitude":149.13551,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:11:50+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464105501,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445915210,"messageType":"UNLIMITED-TRACK","latitude":-35.64789,"longitude":149.16358,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T03:06:50+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464101543,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445914618,"messageType":"UNLIMITED-TRACK","latitude":-35.79567,"longitude":149.164,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T02:56:58+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464100848,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445914356,"messageType":"UNLIMITED-TRACK","latitude":-35.85545,"longitude":149.159,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T02:52:36+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464099729,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445914020,"messageType":"UNLIMITED-TRACK","latitude":-35.93443,"longitude":149.14832,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T02:47:00+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464098656,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445913720,"messageType":"UNLIMITED-TRACK","latitude":-35.99512,"longitude":149.14024,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T02:42:00+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464097685,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445913420,"messageType":"UNLIMITED-TRACK","latitude":-36.06444,"longitude":149.15785,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T02:37:00+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464096538,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445913120,"messageType":"UNLIMITED-TRACK","latitude":-36.13341,"longitude":149.14516,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T02:32:00+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464095566,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445912820,"messageType":"UNLIMITED-TRACK","latitude":-36.20546,"longitude":149.15779,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T02:27:00+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464094635,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445912523,"messageType":"UNLIMITED-TRACK","latitude":-36.2339,"longitude":149.12524,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T02:22:03+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464088627,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445910814,"messageType":"UNLIMITED-TRACK","latitude":-36.2343,"longitude":149.125,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:53:34+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464087388,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445910496,"messageType":"UNLIMITED-TRACK","latitude":-36.23421,"longitude":149.12494,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:48:16+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464086179,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445910198,"messageType":"UNLIMITED-TRACK","latitude":-36.23412,"longitude":149.12503,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:43:18+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464085139,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445909900,"messageType":"UNLIMITED-TRACK","latitude":-36.23065,"longitude":149.13393,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:38:20+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464084097,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445909603,"messageType":"UNLIMITED-TRACK","latitude":-36.20473,"longitude":149.18015,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:33:23+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464083098,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445909306,"messageType":"UNLIMITED-TRACK","latitude":-36.19275,"longitude":149.26016,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:28:26+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464082096,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445909008,"messageType":"UNLIMITED-TRACK","latitude":-36.17796,"longitude":149.33478,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:23:28+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464080863,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445908711,"messageType":"UNLIMITED-TRACK","latitude":-36.16394,"longitude":149.38853,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:18:31+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464078674,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445908116,"messageType":"UNLIMITED-TRACK","latitude":-36.22205,"longitude":149.453,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:08:36+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464077782,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445907875,"messageType":"UNLIMITED-TRACK","latitude":-36.25075,"longitude":149.46568,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T01:04:35+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464076447,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445907521,"messageType":"UNLIMITED-TRACK","latitude":-36.29175,"longitude":149.49706,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T00:58:41+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464074638,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445907066,"messageType":"UNLIMITED-TRACK","latitude":-36.31458,"longitude":149.51871,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T00:51:06+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464072740,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445906629,"messageType":"UNLIMITED-TRACK","latitude":-36.30792,"longitude":149.53412,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T00:43:49+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464072770,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445906329,"messageType":"UNLIMITED-TRACK","latitude":-36.29522,"longitude":149.54717,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T00:38:49+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464072771,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445906029,"messageType":"UNLIMITED-TRACK","latitude":-36.28672,"longitude":149.56039,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T00:33:49+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464069450,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445905738,"messageType":"UNLIMITED-TRACK","latitude":-36.28976,"longitude":149.57068,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-27T00:28:58+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464060210,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445903953,"messageType":"UNLIMITED-TRACK","latitude":-36.26187,"longitude":149.65543,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-26T23:59:13+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":464059043,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445903656,"messageType":"UNLIMITED-TRACK","latitude":-36.2618,"longitude":149.65604,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-26T23:54:16+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":463751797,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445837736,"messageType":"UNLIMITED-TRACK","latitude":-36.2618,"longitude":149.65614,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-26T05:35:36+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":463751009,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445837439,"messageType":"UNLIMITED-TRACK","latitude":-36.26195,"longitude":149.65622,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-26T05:30:39+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":463751022,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445837139,"messageType":"UNLIMITED-TRACK","latitude":-36.26195,"longitude":149.65622,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-26T05:25:39+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":463751023,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445836839,"messageType":"UNLIMITED-TRACK","latitude":-36.26174,"longitude":149.65571,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-26T05:20:39+0000","batteryState":"GOOD","hidden":0},{"@clientUnixTime":"0","id":463743223,"messengerId":"0-2427832","messengerName":"CSGen3 No1","unixTime":1445834462,"messageType":"UNLIMITED-TRACK","latitude":-36.26802,"longitude":149.75372,"modelId":"SPOT3","showCustomMsg":"Y","dateTime":"2015-10-26T04:41:02+0000","batteryState":"GOOD","hidden":0}]}}}}

1 个答案:

答案 0 :(得分:0)

dbc提供的链接提供了答案。我不得不vb.net代码所以这里是转换后的代码和答案:

转换消息以使用自定义JsonConverter

        Public Class Messages
            <JsonProperty("message")>
            <JsonConverter(GetType(SingleOrArrayConverter(Of Message)))>
            Public Property Message() As IList(Of Message)
                Get
                    Return m_Message
                End Get
                Set(value As IList(Of Message))
                    m_Message = value
                End Set
            End Property
            Private m_Message As IList(Of Message)
        End Class

实施SingleOrArrayConverter(VB版本)

      Class SingleOrArrayConverter(Of T)
            Inherits JsonConverter
            Public Overrides Function CanConvert(objectType As Type) As Boolean
                Return (objectType = GetType(List(Of T)))
            End Function

            Public Overrides Function ReadJson(reader As JsonReader, objectType As Type, existingValue As Object, serializer As JsonSerializer) As Object
                Dim token As JToken = JToken.Load(reader)
                If token.Type = JTokenType.Array Then
                    Return token.ToObject(Of List(Of T))()
                End If
                Return New List(Of T)() From {
                    token.ToObject(Of T)()
                }
            End Function

            Public Overrides ReadOnly Property CanWrite() As Boolean
                Get
                    Return False
                End Get
            End Property

            Public Overrides Sub WriteJson(writer As JsonWriter, value As Object, serializer As JsonSerializer)
                Throw New NotImplementedException()
            End Sub
        End Class