Evandro Lacerda
Evandro Lacerda

Reputation: 238

Unexpected character encountered while parsing value: {. Path '', line 1, position 1. at Newtonsoft.Json.JsonTextReader.ReadStringValue?

I'm facing the following error message while trying to convert json string from a http response to a object:

Exception: Unexpected character encountered while parsing value: {. Path '', line 1, position 1. at Newtonsoft.Json.JsonTextReader.ReadStringValue (Newtonsoft.Json.ReadType readType) [0x002d8] in :0 at Newtonsoft.Json.JsonTextReader.ReadAsString () [0x00000] in :0 at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) [0x000c2] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in :0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in :0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in < d32db49e5e3440729da31845c03ddc3a>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in :0 at JmJmApp.Helpers.API.DetalhesChamado (System.String IdentificadorChamado) [0x0004b] in C:\Users..\JmJmApp\JmJmApp\JmJmApp\Helpers\API.cs:262

Here is the code I am using:

        public static object DetalhesChamado(string IdentificadorChamado)
        {
            /* inicio da requisição para a api rest */
            var client = new RestClient("https://api.tomticket.com/chamado/8....8d65/0e8963d....7bfe25612ae78");
            var request = new RestRequest(Method.GET);

             request.RequestFormat = DataFormat.Json;
            //request.AddHeader("content-type", "application/json; charset=utf-8");


            IRestResponse response = client.Execute(request);
            var encoding = response.ContentEncoding;

            if (response.ResponseStatus == ResponseStatus.Completed) // Verifica se foi possivel acessar o servidor
            {
                try
                {
                    //byte[] encodedBytes = Encoding.UTF8.GetBytes(response.Content);

                    var string_json = JsonConvert.DeserializeObject<string>(response.Content);

                    var resposta = JsonConvert.DeserializeObject<ChamadoData>(string_json);


                    //var resposta = JsonConvert.DeserializeObject<ChamadoData>( string_json );

                    return resposta;
                }
                catch (Exception e)
                {

                    return false; // Falha ao deserializar
                }
            }
            else
            {
                return false; // Sem conexão a internet
            }

        }

Here is the response as show by Debug of Visual Studio:

{\"erro\":false,\"data\":{\"idchamado\":\"0e8963dde6ff227c5917bfe25612ae78\",\"protocolo\":80,\"titulo\":\"Veda\\u00e7\\u00e3o e desempenho- Garantia Vencida\",\"mensagem\":\"Mensagem: Est\\u00e1 vazando g\\u00e1s no meu ap\\nNome do Empreendimento :Copacabana\\n Numero da Unidade :43\",\"mimetype\":\"text\\/plain\",\"email_cliente\":\"[email protected]\",\"prioridade\":1,\"tempotrabalho\":0,\"tempoabertura\":null,\"data_criacao\":\"21\\/11\\/2018 10:32\",\"deadline\":null,\"valoritemhora\":0,\"valoritemhoraextra\":0,\"valorfinal\":0,\"valorfinalextra\":0,\"valorfinalbruto\":0,\"nomecliente\":\"cliente teste\",\"tipochamado\":\"Externo\",\"avaliadoproblemaresolvido\":null,\"avaliadoatendimento\":null,\"avaliacaocomentario\":null,\"dataencerramento\":null,\"ultimasituacao\":3,\"dataultimasituacao\":\"21\\/11\\/2018 10:36\",\"descsituacao\":\"Respondido pelo cliente, aguardando resposta\",\"categoria\":null,\"departamento\":\"Suporte Usu\\u00e1rio\",\"atendente\":null,\"id_cliente\":\"[email protected]\",\"status\":null,\"dataultimostatus\":null,\"nomeorganizacao\":null,\"campospersonalizados\":[],\"campospersonalizados_finalizados\":[],\"anexos\":[],\"historico\":[{\"origem\":\"C\",\"mensagem\":\"Passado 72 horas e ainda n\\u00e3o recebi retorno\",\"mimetype\":\"text\\/plain\",\"atendente\":null,\"data_hora\":\"21\\/11\\/2018 10:36\",\"hora_inicio\":null,\"hora_fim\":null,\"anexos\":[]}],\"historico_status\":[]}}"

enter image description here Here are the classes I am using as target.

public class Historico
{
    public string origem { get; set; }
    public string mensagem { get; set; }
    public string mimetype { get; set; }
    public object atendente { get; set; }
    public string data_hora { get; set; }
    public object hora_inicio { get; set; }
    public object hora_fim { get; set; }
    public IList<object> anexos { get; set; }
}

public class ChamadoData
{
    public string idchamado { get; set; }
    public int protocolo { get; set; }
    public string titulo { get; set; }
    public string mensagem { get; set; }
    public string mimetype { get; set; }
    public string email_cliente { get; set; }
    public int prioridade { get; set; }
    public int tempotrabalho { get; set; }
    public object tempoabertura { get; set; }
    public string data_criacao { get; set; }
    public object deadline { get; set; }
    public int valoritemhora { get; set; }
    public int valoritemhoraextra { get; set; }
    public int valorfinal { get; set; }
    public int valorfinalextra { get; set; }
    public int valorfinalbruto { get; set; }
    public string nomecliente { get; set; }
    public string tipochamado { get; set; }
    public object avaliadoproblemaresolvido { get; set; }
    public object avaliadoatendimento { get; set; }
    public object avaliacaocomentario { get; set; }
    public object dataencerramento { get; set; }
    public int ultimasituacao { get; set; }
    public string dataultimasituacao { get; set; }
    public string descsituacao { get; set; }
    public object categoria { get; set; }
    public string departamento { get; set; }
    public object atendente { get; set; }
    public string id_cliente { get; set; }
    public object status { get; set; }
    public object dataultimostatus { get; set; }
    public object nomeorganizacao { get; set; }
    public IList<object> campospersonalizados { get; set; }
    public IList<object> campospersonalizados_finalizados { get; set; }
    public IList<object> anexos { get; set; }
    public IList<Historico> historico { get; set; }
    public IList<object> historico_status { get; set; }
}

public class Historico
{
    public bool erro { get; set; }
    public Data data { get; set; }
}

I've searched for a solution on Google but none worked for me.

Does anyone has any Idea how to solve this issue?

Upvotes: 1

Views: 9132

Answers (1)

Rafi
Rafi

Reputation: 2484

As mentioned in the comments this can be caused by double serializing your json. Sometime this happens because your passing the json as a string over the wire. So you encode into a string and then REST framework you're using will serialize that again. Therefore on the receiving end you will need to deseralize twice or not pass the string over the wire but the original complex object.

Upvotes: 1

Related Questions