vb.net 如何读取 JSON 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22567431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:11:02  来源:igfitidea点击:

How to Read JSON File

jsonvb.netjson.net

提问by Mych

I will be using the following code to get json from a request.

我将使用以下代码从请求中获取 json。

        Dim url As String = urlbuilder.ToString
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim o As JObject = JObject.Parse(reader.ReadToEnd)

        reader.Close()
        response.Close()

I've checked this out and the o (JObject) has the information.

我已经检查过了,o (JObject) 有信息。

I now need to play around with this data but in order not to incur costs while developing (the data is from a paid service) I have a sample of the output that I've save as a file. How do I amend the above code so that I temporarily read in the file instead of process a response. Ideally I would want it still as an JObject.

我现在需要处理这些数据,但为了在开发时不产生成本(数据来自付费服务),我有一个输出样本,已保存为文件。如何修改上面的代码,以便我临时读入文件而不是处理响应。理想情况下,我希望它仍然是 JObject。

采纳答案by Brian Rogers

To read from a file on disk, replace your code with this:

要读取磁盘上的文件,请将代码替换为:

Dim json As String = File.ReadAllText(@"c:\wherever\whatever.txt")
Dim o As JObject = JObject.Parse(json)