【问题标题】:Deserializing Json array dictionary pair反序列化 Json 数组字典对
【发布时间】:2019-07-27 12:47:31
【问题描述】:

我已经检查了这个Deserializing a JSON dictionaryDeserialize json to list of KeyValue pairs

他们并没有完全回答我的问题。我的 JSON 数据格式为

[{"Question":{"Id":1,"SecretQuestion":"Which city were you born"},"Id":1,"SecretAnswer":"ABCD"}]

我有课

<JsonProperty(PropertyName:="secretQuestion")>
    Private _secretQuestion As String

    <JsonProperty(PropertyName:="secretAnswer")>
    Private _secretAnswer As String

    <JsonProperty(PropertyName:="hintsId")>
    Private _hintsId As Integer

    <JsonIgnore>
    Public Property SecretQuestion() As String
        Get
            Return Me._secretQuestion
        End Get
        Set
            Me._secretQuestion = Value
        End Set
    End Property

    <JsonIgnore>
    Public Property SecretAnswer() As String
        Get
            Return Me._secretAnswer
        End Get
        Set
            Me._secretAnswer = Value
        End Set
    End Property

    <JsonIgnore>
    Public Property HintsId() As String
        Get
            Return Me._hintsId
        End Get
        Set
            Me._hintsId = Value
        End Set
    End Property

存储秘密问答的值和第一个id。我尝试了一些在 SO 上提出的反序列化方法,但似乎没有一个效果很好

【问题讨论】:

标签: json vb.net json.net


【解决方案1】:

您的 JSON 可以反序列化为 List(Of class),使用两个简单的类:

Public Class Questions
    Public Property Question As Question
    Public Property Id As Long
    Public Property SecretAnswer As String
End Class

Public Class Question
    Public Property Id As Long
    Public Property SecretQuestion As String
End Class

由于您从服务接收 JSON,我假设它是通常的字符串格式。
在这里,我称它为 JSONObject

Dim myQuestions As List(Of Questions) = JsonConvert.DeserializeObject(Of List(Of Questions))(JSONObject)

现在,myQuestions 将包含Questions 类的列表。
您可以访问其成员,例如:

Dim id As Long = myQuestions(0).Id
Dim answer As String = myQuestions(0).SecretAnswer
Dim question As String = myQuestions(0).Question.SecretQuestion

或者使用LINQ搜索一个,通过ID,例如:

Dim aQuestion As Questions = myQuestions.FirstOrDefault(Function(obj) obj.Id = 0)

要将新问题添加到列表中然后序列化,您只需创建一个新的Questions 对象,设置参数,将其添加到列表中并使用JsonConvert.SerializeObject()List(Of Questions)序列化为JSON字符串的方法:

Dim newQuestionId As Long = myQuestions.Last().Id + 1
Dim newQuestion As Questions = New Questions() With {
    .Id = newQuestionId,
    .SecretAnswer = "A secret answer",
    .Question = New Question() With {
        .Id = newQuestionId,
        .SecretQuestion = "A secret question"
    }
}

myQuestions.Add(newQuestion)

Dim jsonQuestions As String = JsonConvert.SerializeObject(myQuestions)

请注意,使用 Dim newQuestionId = myQuestions.Last().Id + 1,我假设问题 ID 是有序的。如果可能不是,则使用 LINQ 对列表进行排序并取最高 ID:

Dim newQuestionId As Long = myQuestions.OrderBy(Function(obj) obj.Id).Last().Id + 1

如果需要,您可以上传 JSON 字符串 (jsonQuestions)。

基础 JSON 对象:

[
   {
      "Question":{
         "Id":1,
         "SecretQuestion":"Which city were you born"
      },
      "Id":1,
      "SecretAnswer":"ABCD"
   }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多