Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path ‘user’,

publicado por: Anonymous

Hola pues estoy tratando de recibir un json y deserializarlo, pero tengo algunos problmeas …. me está apareciendo el siguiente error :

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path ‘user’, line 1, position 9

//Aqui mando todo ...
var content = new FormUrlEncodedContent(values);

            var response = await client.PostAsync("http://miapi.com/auth/signin",
                content);


                    var responseString = await response.Content.ReadAsStringAsync();
                    activityIndicator.IsRunning = false;
                    activityIndicatorLabel.IsVisible = false;


                    UserJson results = JsonConvert.DeserializeObject<UserJson>(responseString);

Este es el json que recibo y la clase con la que cacho.

{
    "user": {
        "id": 2,
        "created_at": "2019-04-24T23:24:08.000Z",
        "username": "ed",
        "name": "..",
        "lastnameP": "..",
        "lastnameM": "..",
        "pass": "$2a$10$7dDvKc5IM18y9qpnaz6HMetUS.wS/sPDnORLstrADbIMkcgFU7iiK",
        "mail": "[email protected]",
        "admin": 1,
        "user": null,
        "owner": null,
        "gender": null,
        "datenac": "2001-01-01T00:00:00.000Z",
        "phone": "..",
        "calle": "..",
        "colonia": "..",
        "estado": null,
        "cp": 0,
        "img": "165221da670877ef335069b14e8767aaaa32.jpg",
        "id_usercreated": 0,
        "status": "1"
    }
}



using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace App_Intrustfund.Classes
{
    public class UserJson
    {
        public int Id  { set; get; }
        public string Created_at { set; get; }
        public string Username { set; get; }
        public string Name { set; get; }
        public string LastnameP { set; get; }
        public string LastnameM { set; get; }
        public string Pass { set; get; }
        public string Mail { set; get; }
        public int Admin { set; get; }
        public int User { set; get; }
        public int Owner { set; get; }
        public string Gender { set; get; }
        public string Datenac { set; get; }
        public string Phone { set; get; }
        public string Calle { set; get; }
        public string Colonia { set; get; }
        public string Estado { set; get; }
        public string Cp { set; get; }
        public string Img { set; get; }
        public int Id_usercreated { set; get; }
        public int Status { set; get; }
    }

}

solución

El error parece estar al momento de deserializarlo ya que el json que recibes contiene un nodo “user” el cual no existe en tu clase UserJson por lo tanto deberías dividir tu clase en 2 de la siguiente manera

public class User
{
  public int id { get; set; }
  public DateTime created_at { get; set; }
  public string username { get; set; }
  public string name { get; set; }
  public string lastnameP { get; set; }
  public string lastnameM { get; set; }
  public string pass { get; set; }
  public string mail { get; set; }
  public int admin { get; set; }
  public object user { get; set; }
  public object owner { get; set; }
  public object gender { get; set; }
  public DateTime datenac { get; set; }
  public string phone { get; set; }
  public string calle { get; set; }
  public string colonia { get; set; }
  public object estado { get; set; }
  public int cp { get; set; }
  public string img { get; set; }
  public int id_usercreated { get; set; }
  public string status { get; set; }
}

public class UserJson
{
  public User user { get; set; }
}

Y tratar de hacer la deserialización de esta forma

var results = JsonConvert.DeserializeObject<UserJson>(responseString);
Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *