0

hello I have this json code and I would like to get the value which is 21, black jack, tongits along with its score.

{
  "id": 1,
  "status": "PARTIAL",
  "scores": [
    {
      "scoreCard": {
        "21": 0,
        "black jack": 0,
        "tongits": 0
      },
      "question": "Favorite game",
      "questionnaireId": 1
    },
    {
      "scoreCard": {
        "red": 0,
        "green": 0,
       "blue": 0,
        "black": 0
      },
      "question": "Favorite color",
      "questionnaireId": 2
    }
  ]
}

this is my vb code which I only get the value Favorite game and Favorite color

Dim webC As New System.Net.WebClient
     Dim json As String =webC.DownloadString("http://localhost:8080/report/survey/1/result")

    Dim o As JObject = JObject.Parse(json)

    Dim results As List(Of JToken) = o.Children().ToList

    For Each item As JProperty In results
        item.CreateReader()
        Select Case item.Name

            Case "scores"

                Dim question As String

                For Each subitem As JObject In item.Values
                    question = subitem("question")
                    Listview2.Items.Add(question)

                Next
        End Select
    Next
2
  • you could deserialize it in 1-2 lines of code Commented Feb 12, 2017 at 14:03
  • what exacly do you mean? Commented Feb 12, 2017 at 14:20

1 Answer 1

0

Its easier to parse the data to get just one or two things:

Dim jstr = ...from where ever...

Dim jData = JObject.Parse(jstr)

Console.WriteLine(jData("scores")(0)("scoreCard")("21"))
Console.WriteLine(jData("scores")(0)("scoreCard")("black jack"))
Console.WriteLine(jData("scores")(0)("scoreCard")("tongits"))

Console.WriteLine(jData("scores")(0)("question"))
Console.WriteLine(jData("scores")(1)("question"))

scores is an array as indicated by the [ ... ] enclosure, so that needs to be indexed, otherwise just use the indentation and intellisence to fish out whatever you want. Results:

0
0
0
Favorite game
Favorite color

However, since you are after 4 things at least: the 2 questions and 3 scores, it would be even easier to deserialize the data to a class object and just get what you want from the properties.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.