如何使用Newtonsoft.JSON获取不同类型的REST值

时间:2017-06-28 09:33:38

标签: json vb.net json.net

这是我从服务器获得的REST响应的一部分(希望只是我的问题的相关部分):

{
   "documents" : [
      {
         "fields" : [
            {
               "definition" : {
                  "href" : "https://www.../FAnZtVk-s5A",
                  "rel" : "field_definition",
                  "title" : "Title of first field"
               },
               "formatted" : "TheFormattedValue1",
               "value" : "Value1"
            },
            {
               "definition" : {
                  "href" : "https://www/yggc2yT69vY",
                  "rel" : "field_definition",
                  "title" : "Title of second field"
               },
               "formatted" : "TheFormattedValue2",
               "value" : "Value2"
            },
            {
               "definition" : {
                  "href" : "https://www/ptM7kdddz_M",
                  "rel" : "field_definition",
                  "title" : "Title of third field"
               },
               "formatted" : [
                  {
                     "icon" : "starred",
                     "text" : "Red"
                  }
               ],
               "value" : [
                  "-"
               ]
            },

第一个和第二个字段“formatted”和“value”是字符串类型,第三个字段是图标/文本数组。

这是我的.net类代码:

Public Class clsRestDokumentenliste
    Public Property documents As List(Of Document)
    Public Property links As List(Of link)
    Public Property meta As Meta
End Class

Public Class Document
    Public Property fields As List(Of Field)
    Public Property forwarded As String
    Public Property invalid As String
End Class

Public Class Field
    Public Property definition As List(Of Definition)
    Public Property formatted As String
    Public Property value As String
End Class

加载数据时出现错误,因为“Field”类的“格式化”属性有时是字符串,有时是其他类型的图标/文本

Dokumente = Newtonsoft.Json.JsonConvert.DeserializeObject(Of clsRestDokumentenliste)(JSONstring)

我认为必须更改“List(of Field)”中Field的定义。 我如何定义我的类来解析它?

0 个答案:

没有答案