Mykel
Mykel

Reputation: 129

Deserializing Json array dictionary pair

I have checked this Deserializing a JSON dictionary and Deserialize json to list of KeyValue pairs

They don't quite answer my question. My JSON data is in the form

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

I have a class

<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

To store the value of secret question and answer and the first id. I have tried some deserialization methods proposed on SO but none seems to work well

Upvotes: 1

Views: 137

Answers (1)

Jimi
Jimi

Reputation: 32248

Your JSON can be deserialized into a List(Of class), using two simple classes:

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

Since you're receiving the JSON from a Service, I assume it's the usual string format.
Here, I'm calling it the JSONObject:

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

Now, myQuestions will contain a list of Questions classes.
You can the access its members with, for example:

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

Or use LINQ to search one, by ID, for example:

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

To add a new question to the list and then serialize it, you just need to create a new Questions object, set the parameters, add it to the list and use the JsonConvert.SerializeObject() method to serialize the List(Of Questions) into a JSON string:

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)

Note that, using Dim newQuestionId = myQuestions.Last().Id + 1, I'm supposing that the questions IDs are ordered. If they may be not, then use LINQ to order the list and take the highest ID:

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

You can then upload the JSON string (jsonQuestions), if it's a requirement.

The base JSON object:

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

Upvotes: 1

Related Questions