【问题标题】:Problems with Json deserialisation within .NET.NET 中的 Json 反序列化问题
【发布时间】:2016-06-17 01:36:08
【问题描述】:

我构建了以下要存储数据的类(帮助也可以在 c# 中)

Public Class DatasourceInfos
    Public Property DSContainer() As List(Of DatasourceInfo)
End Class

Public Class DatasourceInfo
    Public Property name As String
    Public Property description As String
    Public Property created As String
    Public Property modified As String
    Public Property identifier As String
    Public Property href As String
    Public Property cacheAvailable As Boolean
    Public Property id As ULong
End Class

我接收json格式的数据如下:

[{  "name":"MyName1",
    "description":"MyDescription1",
    "created":1244193265000,
    "modified":1264515442000,
    "identifier":"Identifier==",
    "href":"https://....",
    "cacheAvailable":true,
    "id":29},

    {"name":"MyName2",
    "description":"MyDescription2",
    "created":1244193265000,
    "modified":1264515442000,
    "identifier":"Identifier==",
    "href":"https://....",
    "cacheAvailable":true,
    "id":30}]

使用RestSharp(解决方案不得使用RestSharp)我尝试通过以下方式将数据放入我的班级:

Public Function getDatasources(ByVal _token As String) As String
    Dim client = New RestClient(_baseURI)
    Dim request = New RestRequest("/data", Method.GET)
    request.AddHeader("Authorization", "Basic " + _token)
    Dim response = client.Execute(Of DatasourceInfos)(request)
    Return response
End Function

但是查看response object 并没有映射到我的课程。谁能告诉我我做错了什么?我将这段代码与SO 上的许多其他代码进行了比较,但我就是看不出有什么问题。

【问题讨论】:

    标签: c# json vb.net deserialization restsharp


    【解决方案1】:

    在显示的 json 中,没有任何与您的 DatasourceInfos 容器类相关的内容。为此,json 看起来像:

    {
        "DSInfoItems":
         ...all your json ....
    }
    

    至少,JSON.NET 在尝试反序列化为 DatasourceInfos 时对格式大喊大叫,但会轻松反序列化为 DatasourceInfo 的数组或列表:

    Dim DS = JsonConvert.DeserializeObject(Of List(Of DataSource))(jstr)
    

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2021-05-28
      相关资源
      最近更新 更多