简体   繁体   中英

How to Read JSON File

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

        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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM