Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse nested JSON with json.net in Visual Basic

I have nested JSON strings that i would like to parse out the appropriate values from much like below. As i am learning by doing I am struggling a little bit, I have the first part working in that I can parse out single JSON strings, and return the appropriate value using code example 1 below, however i am stuck with a JSON string that is problematic in that it is nested, so the same approach won't work

{
  "jsonrpc":"2.0",
  "method":"Player.OnPause",
  "params":{
     "data": { "item": { "id":29, "type":"episode" },
               "player": { "playerid":1, "speed":0 }
             },
     "sender":"xbmc"
  }
}

And the code...

    Dim JSON As String
    Dim values As Newtonsoft.Json.Linq.JObject
    JSON = JSON STRING WOULD GO HERE, COMES from TCP IP STREAM
    values = JObject.Parse(JSON)
    Console.WriteLine(values.GetValue("method"))

Using that example i can extract the method key (e.g. Player.OnPause) from the first level JSON string, but how can i extract data from the second, and third level strings, For example in the above string, being able to get to Data level JSON values, and Item level JSON values. Is this possible in a similar way to the above?

Appreciate any tips you could provide, I am a learn by examples person, but just struggling to apply something to read multiple nested JSON strings, or multiple levels. No doubt it will be an easy thing that i am missing, but id appreciate any help someone could provide.

Thanks

like image 344
user1829564 Avatar asked Oct 27 '25 10:10

user1829564


1 Answers

    Dim jsonstring = IO.File.ReadAllText("json.txt")
    Dim jo = Json.Linq.JObject.Parse(jsonstring)
    Dim playerid = jo("params")("data")("player")("playerid")

Do you mean something like this? "json.txt" simply contains your JSON string.

like image 54
igrimpe Avatar answered Oct 29 '25 02:10

igrimpe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!