Read an internet page in a variable in VB

Asked

Viewed 415 times

2

Guys I’m trying to read a website and I’m not getting it, I want you to save all the content in a variable, the problem is that it doesn’t have any html or xml, it’s just text, someone would know how to do this and also how to search for a text in it?

  • That page would be a JSON right ? You would have to look how to serialize a Json with vb.net

  • how could I do that?

  • Were any of the answers helpful?

2 answers

2


To serialize a Json in Vb.Net Datacontractattribute.

First you would have to create an object to receive this JSON, realize that your example pussui objects inside the Json own, example:

"atividades_secundarias": [
    {
      "text": "Reprodução de vídeo em qualquer suporte",
      "code": "18.30-0-02"
    },

In this case it is an object called atividades_secundarias with two attributes text and code. What alias is a Array

To Serialize you can do :

1.You will create a method that will receive this JSON and do the deserialization

' VB.NET
Private Function DeserializarDataContractJsonSerializer() As List(Of SeuObjeto)
    Using Stream = New System.IO.FileStream(JsonQueVocêPassa, System.IO.FileMode.Open)
        Dim Serializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(List(Of Pedido)))
        Return DirectCast(Serializer.ReadObject(Stream), List(Of SeuObjeto))
    End Using
End Function

JsonQueVocêPassa = https://www.receitaws.com.br/v1/cnpj/27865757000102

SeuObjeto = Object you create with the Json structure you are trying to serialize.

In this example you are reading a Json from a file using and mounting the object.

If you wanted some examples have on the website of Microsoft and also this Tutorial

  • wait, I would copy the function in my code and what would I call it? And it returns as?

  • So, this is an example, you can use this basic example and try to implement for your case, in your case the difference would be that it is not deserializando from a file, but rather from this link, which can be a API REST, try to get into those links I sent you and see what you don’t understand.

  • And why mutias sometimes lack some knowledge about JSON or how it works this returns that you are receiving, then it is difficult for you to understand and do right.

1

Catch the Json

In the json variable you will have the json object obtained from the URL.

   Dim json As String = New System.Net.WebClient().DownloadString("https://www.receitaws.com.br/v1/cnpj/27865757000102")

Convert Json to Xml

-Download a dll Newtonsoft: http://www.newtonsoft.com/json

-Use the dll Newtonsoft

Dim doc As XmlDocument = JsonConvert.DeserializeXmlNode(json, "root")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.