How do you perform this json on vb.net?

2

I am using the Graph API of Facebook with webhook to channel an incoming message from a page in my application. The response received from the API is as follows:

{   "object":"page",
    "entry": [{"id":"594052300785675",
            "time":1495551653413,
            "messaging":[{
                "sender": {"id":"1501979286539065"},
                "recipient":{"id":"594052300785675"},
                "timestamp":1495551624095,
                "message":{
                    "mid":"mid.$cAAJGvf9bkd5iZxgLn1cNdDAgNL_l",
                    "seq":505,
                    "text":"HOLA HOLA HOLA POKEMO GOKU DRAGON BALL"
                    }
                }]
        }]
}

How can I get the data text and then save it in my database? I'm using framework 4.0 from vb.net

    
asked by lucasalderete 23.05.2017 в 17:28
source

2 answers

3

You can do the following:

Dim json As JObject = JObject.Parse(strJson);

Where strJson is what you get from the api

And then go through it

For Each Row In json("entry")
    //Row("messaging")
Next

And explore its properties

    
answered by 23.05.2017 / 17:34
source
2

Although there is already an accepted response, as it seems that there are still people who do not know it, I will explain two ways to create classes to deserialize JSON.

Option A

There are different online options, for example json2csharp.com (for c #). Just paste the example json, press Generate and you're done

Option B

In Visual Studio there is an option for several versions to autogenerate classes for both json and XML. It is very simple. Copy the sample json / xml, and let Editar , Pegado Especial , Pegar JSON como clases and automatically generate the necessary classes.

Taking the JSON of the question as an example, this is the result:

Public Class Rootobject
    Public Property _object As String
    Public Property entry() As Entry
End Class

Public Class Entry
    Public Property id As String
    Public Property time As Long
    Public Property messaging() As Messaging
End Class

Public Class Messaging
    Public Property sender As Sender
    Public Property recipient As Recipient
    Public Property timestamp As Long
    Public Property message As Message
End Class

Public Class Sender
    Public Property id As String
End Class

Public Class Recipient
    Public Property id As String
End Class

Public Class Message
    Public Property mid As String
    Public Property seq As Integer
    Public Property text As String
End Class
    
answered by 23.05.2017 в 18:09