在Visual Basic中使用json.net解析嵌套的JSON [英] Parse nested JSON with json.net in Visual Basic

查看:70
本文介绍了在Visual Basic中使用json.net解析嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有嵌套的JSON字符串,我想从下面的内容中解析出适当的值.在学习过程中,我有点挣扎,我正在做的第一部分工作是可以解析单个JSON字符串,并使用下面的代码示例1返回适当的值,但是我仍然坚持使用JSON字符串有问题的是它是嵌套的,所以相同的方法行不通

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"
  }
}

还有代码...

    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"))

使用该示例,我可以从第一级JSON字符串中提取方法密钥(例如Player.OnPause),但是我如何从第二级和第三级字符串中提取数据,例如,在上述字符串中,能够转到数据级JSON值和项目级JSON值.是否可以通过与上述类似的方式实现?

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?

感谢您可以提供的任何技巧,我是一个示例学习者,但只是努力地应用一些东西来读取多个嵌套的JSON字符串或多个级别.毫无疑问,这是一件容易的事,但我很感谢别人能提供的任何帮助.

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.

谢谢

推荐答案

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

您的意思是这样的吗? "json.txt"仅包含您的JSON字符串.

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

这篇关于在Visual Basic中使用json.net解析嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆