Problemas com Web Service vb.NET convertendo DataSet em JSON e retornando

3 respostas
aboult

Pessoal, boa tarde.

Eu havia feito um web service onde eu retornava um DataSet que depois era consumido via SOAP no Android.
Como ficou extremamente pesado e me indicaram utilizar JSON, estou alterando.

<WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetClientes2(ByVal representante As Integer)
        Dim wCampos As String = "", wCondicao As String = ""
        Dim dr As DataSet = Nothing
        Dim jss As New JavaScriptSerializer
        Dim jsonString As String
        'Dim jsonString As String = jss.Serialize(DataSet1.GetXML())

        Try
            If fBuscaRelacao(representante, C_TAB_CLIENTE) Then
                dr = wsDados.execSelectDataSet("SELECT Identificacao, RazaoSocial, NomeFantasia, Inscricao, Contato, Obs, Representante, TabelaPreco FROM Cliente WHERE Representante = " & representante & " AND Sincronizado = 0")
                If Not dr Is Nothing Then
                    wCondicao = "Representante = " & representante
                    wCampos = "DataUltSincronizacao = " & wsDados.dataSql(CDate(Date.Today)) & ", Sincronizado = " & 1
                    wsDados.execUpdate("Cliente", wCampos, wCondicao)
                End If
            End If
        Catch ex As Exception
            fExtornaImportacao(representante, C_TAB_CLIENTE)
            'Return dr
            jsonString = jss.Serialize(dr.GetXml())
            Return jsonString
        Finally
            fAtualizaRelacao(representante, C_TAB_CLIENTE)
        End Try
        jsonString = jss.Serialize(dr.GetXml())
        Return jsonString
    End Function

Mas o retorno pelo browser saí dessa maneira:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

"\u003cNewDataSet\u003e\r\n \u003ctabelaTemp\u003e\r\n \u003cIdentificacao\u003e8268\u003c/Identificacao\u003e\r\n \u003cRazaoSocial\u003eDROGATEM MEDICAMENTOS E PERFUMARIA LTDA\u003c/RazaoSocial\u003e\r\n \u003cNomeFantasia\u003eDROGATEM MEDICAMENTOS E PERFUMARIA LTDA\u003c/NomeFantasia\u003e\r\n \u003cInscricao\u003e24878241000104\u003c/Inscricao\u003e\r\n \u003cContato /\u003e\r\n \u003cObs /\u003e\r\n \u003cRepresentante\u003e114\u003c/Representante\u003e\r\n \u003cTabelaPreco\u003e78\u003c/TabelaPreco\u003e\r\n \u003c/tabelaTemp\u003e\r\n \u003ctabelaTemp\u003e\r\n \u003cIdentificacao\u003e8649\u003c/Identificacao\u003e\r\n \u003cRazaoSocial\u003eDROGATEM MED. PERF. ITUMBIARA LTDA\u003c/RazaoSocial\u003e\r\n \u003cNomeFantasia\u003eDROGATEM MED. PERF. ITUMBIARA LTDA\u003c/NomeFantasia\u003e\r\n \u003cInscricao\u003e24878241000449\u003c/Inscricao\u003e\r\n \u003cContato /\u003e\r\n \u003cObs /\u003e\r\n \u003cRepresentante\u003e114\u003c/Representante\u003e\r\n \u003cTabelaPreco\u003e78\u003c/TabelaPreco\u003e\r\n \u003c/tabelaTemp\u003e\r\n \u003ctabelaTemp\u003e\r\n \u003cIdentificacao\u003e8650\u003c/Identificacao\u003e\r\n \u003cRazaoSocial\u003eDROGATEM MEDICAMENTOS E PERFUMARIA LTDA\u003c/RazaoSocial\u003e\r\n \u003cNomeFantasia\u003eDROGATEM MEDICAMENTOS E PERFUMARIA LTDA\u003c/NomeFantasia\u003e\r\n \u003cInscricao\u003e24878241000368\u003c/Inscricao\u003e\r\n \u003cContato /\u003e\r\n \u003cObs /\u003e\r\n

É gigante então copiei apenas uma parte...
Alguém tem ideia do que falta eu mudar ou se não é possível eu fazer essa conversão direta da maneira que eu estou tentando?

Obrigado desde já.

3 Respostas

Nicolas_Fernandes

E aí, aboult, beleza?

Cara, que tal se você usar o JSON.NET para automagicamente converter os DataTables do seu DataSet em um resultado JSON?
Daí você não precisa converter o seu DataSet em um XML e depois serializá-lo.
Faz o teste aí!

aboult

Nicolas Fernandes:
E aí, aboult, beleza?

Cara, que tal se você usar o JSON.NET para automagicamente converter os DataTables do seu DataSet em um resultado JSON?
Daí você não precisa converter o seu DataSet em um XML e depois serializá-lo.
Faz o teste aí!

E ae, blz e você?

Ok vou olhar sim.

Mas então da maneira que eu estou tentando implementar, não dá certo?

Ou ele não está dando certo por outro motivo?

É porque eu vi em outros fóruns o pessoal falando para fazer dessa maneira, mas parece que ele continua tentando jogar o xml ao invés de converter para JSON.

aboult

Nicolas Fernandes:
E aí, aboult, beleza?

Cara, que tal se você usar o JSON.NET para automagicamente converter os DataTables do seu DataSet em um resultado JSON?
Daí você não precisa converter o seu DataSet em um XML e depois serializá-lo.
Faz o teste aí!

Pronto estou utilizando o JSON.NET

Fiz a conversão dessa maneira o dr é meu DataSet:

jsonString = JsonConvert.SerializeObject(dr) Return jsonString

Eu não entendo muito bem de JSON e Web Service… é o primeiro que estou fazendo.

O resultado saiu dessa maneira(Copiei apenas um pedaço):

This XML file does not appear to have any style information associated with it. The document tree is shown below.

{“tabelaTemp”:[{“Identificacao”:233,“RazaoSocial”:“ADALBERTO MARTINS”,“NomeFantasia”:“EMPORIO SAO JUDAS TADEU “,“Inscricao”:“44184638000103”,“Contato”:””,“Obs”:"",“Representante”:113,“TabelaPreco”:79},{“Identificacao”:235,“RazaoSocial”:“ANDRADE MIL DROGAS 3 JARDINS LTDA ME”,“NomeFantasia”:“ANDRADE MIL DROGAS 3 JARDINS LTDA ME”,“Inscricao”:“62048863000196”,“Contato”:"",“Obs”:"",“Representante”:113,“TabelaPreco”:79},{“Identificacao”:237,“RazaoSocial”:“DROGARIA PRUDENFARMA LTDA”,“NomeFantasia”:“PRUDENFARMA”,“Inscricao”:“01913782000112”,“Contato”:"",“Obs”:"",“Representante”:113,“TabelaPreco”:79},{“Identificacao”:238,“RazaoSocial”:“COMERCIAL DIST. E MEDICAMENTOS LTDA”,“NomeFantasia”:“COMERCIAL DIST. E MEDICAMENTOS LTDA”,“Inscricao”:“67080499000165”,“Contato”:"",“Obs”:"",“Representante”:113,“TabelaPreco”:79},{“Identificacao”:239,“RazaoSocial”:“DIAS PASTORINHO S/A COM. & IND”,“NomeFantasia”:“DIAS PASTORINHO S/A COM. & IND”,“Inscricao”:“61192795000333”,“Contato”:"",“Obs”:"",“Representante”:113,“TabelaPreco”:79},{“Identificacao”:240,“RazaoSocial”:“DIAS PASTORINHO S/A COM. & IND.”,“NomeFantasia”:“DIAS PASTORINHO S/A COM. & IND.”,“Inscricao”:“61192795001577”,“Contato”:"",“Obs”:"",“Representante”:113,“TabelaPreco”:79},{“Identificacao”:241,“RazaoSocial”:“DROGA BRUMA FARMACIA LTDA”,“NomeFantasia”:"DROGA

É isso mesmo que deveria ser o retorno, correto?

Criado 26 de abril de 2013
Ultima resposta 26 de abr. de 2013
Respostas 3
Participantes 2