How to read JSON http post response using VB

前端 未结 4 1477
天命终不由人
天命终不由人 2020-12-14 10:54

I have the following code, it connects to PHP server and retrieve data successfully, i\'m not very good with VB, how can i read the JSON response text and extract it\'s elem

相关标签:
4条回答
  • 2020-12-14 11:35

    To use

    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    

    'Json.Net' library should be installed.

    screenshot

    0 讨论(0)
  • 2020-12-14 11:46

    After long research and many tests I found out a very nice extension called Newtonsoft.json, it's extremely simple and can be installed from package manager console like this:

    install-package Newtonsoft.json
    

    And include it like this:

    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    

    Then all i needed to do is to declare the elements names and values like this:

    Else
        Dim json As String = responseFromServer
        Dim ser As JObject = JObject.Parse(json)
        Dim data As List(Of JToken) = ser.Children().ToList
        Dim output As String = ""
    
        For Each item As JProperty In data
            item.CreateReader()
            Select Case item.Name
                Case "comments"
                    output += "Comments:" + vbCrLf
                    For Each comment As JObject In item.Values
                        Dim u As String = comment("user")
                        Dim d As String = comment("date")
                        Dim c As String = comment("comment")
                        output += u + vbTab + d + vbTab + c + vbCrLf
                    Next
    
                Case "messages"
                    output += "Messages:" + vbCrLf
                    For Each msg As JObject In item.Values
                        Dim f As String = msg("from")
                        Dim t As String = msg("to")
                        Dim d As String = msg("date")
                        Dim m As String = msg("message")
                        Dim s As String = msg("status")
                        output += f + vbTab + t + vbTab + d + vbTab + m + vbTab + s + vbCrLf
                    Next
    
            End Select
        Next
        MsgBox(output)
    End If
    

    hope someone will find this useful

    0 讨论(0)
  • 2020-12-14 11:46
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    

    This seems to cut it on VB.net for youtube API V.3
    of course it depends on what you are trying to accomplish but Youtube returns data as Json format

    0 讨论(0)
  • 2020-12-14 11:48

    @razzak is absolutely right to use the Json.Net NuGet package. Another option that would cut this down dramatically, is to use the built in DeserializeObject function. As long as you have a well define model, then you can deserialize the Json right into an instance of the object using something like this:

    dim myObject as MyDefinedObject = JsonConvert.DeserializeObject(responseFromServer)
    

    or this in C#

    MyDefinedObject m = JsonConvert.DeserializeObject<MyDefinedObject>(responseFromServer);
    

    Also, if you don't want to loop, you could also select tokens using something like this:

    Dim d = ser.SelectToken("$..resources[?(@)].travelDistance")
    

    This code above was used to locate the travelDistance between two points from the Bing API. If you have ever dealt with the Bing or Google Map REST APIs, then you know the JSon is generally too large to loop through the data when you are looking for very specific values.

    I hope this is helpful for anyone looking to do something like this. The JSon.Net website has a blog page that goes through some additional examples.

    http://james.newtonking.com/json

    ~Cheers

    0 讨论(0)
提交回复
热议问题