3
Consider the JSON below
{
"atividade_principal": [
{
"text": "Atividades de televisão aberta",
"code": "60.21-7-00"
}
],
"data_situacao": "03/11/2005",
"nome": "GLOBO COMUNICACAO E PARTICIPACOES S/A",
"uf": "RJ",
"telefone": "(21) 2540-2623",
"atividades_secundarias": [
{
"text": "Reprodução de vídeo em qualquer suporte",
"code": "18.30-0-02"
},
{
"text": "Portais, provedores de conteúdo e outros serviços de informação na internet",
"code": "63.19-4-00"
},
{
"text": "Agenciamento de espaços para publicidade, exceto em veículos de comunicação",
"code": "73.12-2-00"
},
{
"text": "Programadoras",
"code": "60.22-5-01"
}
],
"qsa": [
{
"qual": "10-Diretor",
"nome": "CARLOS HENRIQUE SCHRODER"
},
{
"qual": "10-Diretor",
"nome": "JORGE LUIZ DE BARROS NOBREGA"
},
{
"qual": "10-Diretor",
"nome": "ROSSANA FONTENELE BERTO"
},
{
"qual": "10-Diretor",
"nome": "ALI AHAMAD KAMEL ALI HARFOUCHE"
},
{
"qual": "10-Diretor",
"nome": "WILLY HAAS FILHO"
},
{
"qual": "10-Diretor",
"nome": "JUAREZ DE QUEIROZ CAMPOS JUNIOR"
},
{
"qual": "10-Diretor",
"nome": "SERGIO LOURENCO MARQUES"
},
{
"qual": "10-Diretor",
"nome": "MARCELO LUIS MENDES SOARES DA SILVA"
},
{
"qual": "10-Diretor",
"nome": "ANTONIO CLAUDIO FERREIRA NETTO"
},
{
"qual": "10-Diretor",
"nome": "CRISTIANE DELECRODE LOPES SUT RIBEIRO"
}
],
"situacao": "ATIVA",
"bairro": "JARDIM BOTANICO",
"logradouro": "R LOPES QUINTAS",
"numero": "303",
"cep": "22.460-901",
"municipio": "RIO DE JANEIRO",
"abertura": "31/01/1986",
"natureza_juridica": "205-4 - Sociedade Anônima Fechada",
"fantasia": "GCP,TV GLOBO, REDE GLOBO, GLOBO.COM, SOM LIVRE",
"cnpj": "27.865.757/0001-02",
"ultima_atualizacao": "2016-11-21T09:10:20.052Z",
"status": "OK",
"tipo": "MATRIZ",
"complemento": "",
"email": "",
"efr": "",
"motivo_situacao": "",
"situacao_especial": "",
"data_situacao_especial": "",
"capital_social": "6408935530.37",
"extra": {}
}
Classes to be serialized
using System;
using System.Runtime.Serialization;
namespace ITSolution.Web.JSON
{
[DataContract]
public class DataContractEmpresa
{
[DataMember]
public string[] atividade_principal { get; set; }
[DataMember]
public DateTime data_situacao { get; set; }
[DataMember]
public string nome { get; set; }
[DataMember]
public string uf { get; set; }
[DataMember]
public string telefone { get; set; }
[DataMember]
public string[] atividades_secundarias { get; set; }
[DataMember]
public string[] qsa { get; set; }
[DataMember]
public string situacao { get; set; }
[DataMember]
public string bairro { get; set; }
[DataMember]
public string logradouro { get; set; }
[DataMember]
public string numero { get; set; }
[DataMember]
public string cep { get; set; }
[DataMember]
public string municipio { get; set; }
[DataMember]
public string abertura { get; set; }
[DataMember]
public string natureza_juridica { get; set; }
[DataMember]
public string fantasia { get; set; }
[DataMember]
public string cnpj { get; set; }
[DataMember]
public string ultima_atualizacao { get; set; }
[DataMember]
public string status { get; set; }
[DataMember]
public string tipo { get; set; }
[DataMember]
public string complemento { get; set; }
[DataMember]
public string email { get; set; }
[DataMember]
public string efr { get; set; }
[DataMember]
public string motivo_situacao { get; set; }
[DataMember]
public string situacao_especial { get; set; }
[DataMember]
public string data_situacao_especial { get; set; }
[DataMember]
public string capital_social { get; set; }
[DataMember]
public string extra { get; set; }
}
}
Deserializing:
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
namespace ITSolution.Web.JSON
{
public static class JSONHelper
{
public static string GetJSONString(string url)
{
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(
stream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
public static T GetObjectFromJSONString<T>(
string json) where T : new()
{
using (MemoryStream stream = new MemoryStream(
Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(stream);
}
}
public static T[] GetArrayFromJSONString<T>(
string json) where T : new()
{
using (MemoryStream stream = new MemoryStream(
Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(T[]));
return (T[])serializer.ReadObject(stream);
}
}
}
}
Applying
string url = @"https://www.receitaws.com.br/v1/cnpj/27865757000102";
var json = JSONHelper.GetJSONString(url);
var r = JSONHelper.GetObjectFromJSONString<DataContractEmpresa>(json);
Exception played
An unhandled Exception of type 'System.Runtime.Serialization.Serializationexception' occurred in System.Runtime.Serialization.dll
Additional information: There was an error deserializing the object of type Itsolution.Web.JSON.DataContractEmpresa. End item expected in namespace'. Text element found in namespace ''.`
The problem is serialization, I’m a beginner and I’ve never touched JSON, as I do to get around this error?
If you don’t know how to do a (de)serialization, why not use something as ready as the JSON.NET? I assure you that it is much more complete and easy to use than this helper class there.
– Jéf Bueno
The problem, I believe, is the collections
atividades_secundarias
andcsa
. In JSON are object collections, whereas in c# is an array ofstring
s...– Genos
What’s in the extra key???
– novic