Intento agregar un paciente a la base de datos de firebase.
este es el codigo
Paciente.cs
public class Patient
{
public string ID { get; set; }
public string PatientName { get; set; }
public string PatientGender { get; set; }
public string PatientAddress { get; set; }
public string PatientWeight { get; set; }
public string PatientHeight { get; set; }
public string PatientMobileNO { get; set; }
public string PatientBirthday { get; set; }
public string PatientGenticesDiseses { get; set; }
// public List<Appoitment> Appointments { get; set; }
public ObservableCollection<Appoitment> Appointments;
public Patient()
{
}
public Patient(Patient instans)
{
ID = instans.ID;
PatientName = instans.PatientName;
PatientGender = instans.PatientGender;
PatientAddress = instans.PatientAddress;
PatientWeight = instans.PatientWeight;
PatientHeight = instans.PatientHeight;
PatientMobileNO = instans.PatientMobileNO;
PatientGenticesDiseses = instans.PatientGenticesDiseses;
Appointments = instans.Appointments;
}
}
ServiciosPacientes.cs
public async Task Addpat( Patient patient)
{
var x = await firebaseClient
.Child($"Specalists/406707265/Patients")
.PostAsync(new Patient(patient));
Patient pat = new Patient(x.Object);
pat.ID = x.Key;
await firebaseClient
.Child($"Specalists/406707265/Patients/{pat.ID}")
.PatchAsync(pat);
}
agregarPaciente.xaml.cs
public partial class AddPatientPage: ContentPage
{
public AddPatientPage()
{
InitializeComponent();
AddPatientClick();
}
public async void addPatient()
{
PatientService patientService = new PatientService();
Patient pat = new Patient();
pat.PatientAddress = PatientAddress.Text;
pat.PatientBirthday = PatientBirthday.Text;
pat.PatientGender = PatientGender.Text;
pat.PatientGenticesDiseses = PatientGenticsDiseses.Text;
pat.PatientHeight = PatientHeight.Text;
pat.PatientMobileNO = PatientMobileNo.Text;
pat.PatientName = PatientName.Text;
pat.PatientWeight = PatientWeight.Text;
Random rd = new Random();
int rand_num = rd.Next(1, 4000);
pat.ID = rand_num.ToString();
try
{
await patientService.Addpat(pat);
await Application.Current.MainPage.DisplayAlert("message", "Patient Added", "ok");
await Navigation.PushAsync(new PatientsListPage());
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("error", ex.Message, "ok");
}
}
void AddPatientClick()
{
save.GestureRecognizers.Add(new TapGestureRecognizer()
{
Command = new Command(() =>
{
addPatient();
})
});
}
}
agregarPaciente.xaml
<Entry Placeholder="Patient Address"
x:Name="PatientAddress"></Entry>
<Entry Placeholder="Patient Birthday"
x:Name="PatientBirthday"></Entry>
<Entry Placeholder="Patient Gender"
x:Name="PatientGender"></Entry>
<Entry Placeholder="Patient Gentics Diseses"
x:Name="PatientGenticsDiseses"></Entry>
<Entry Placeholder="Patient Height"
x:Name="PatientHeight"></Entry>
<Entry Placeholder="Patient Mobile Number"
x:Name="PatientMobileNo"></Entry>
<Entry Placeholder="Patient Name"
x:Name="PatientName"></Entry>
<Entry Placeholder="Patient Weight"
x:Name="PatientWeight"></Entry>
<Label x:Name="save" HorizontalOptions="Center" >Save</Label>
El código funciona y Patient
se agrega a Firebase, pero cuando me da la alerta, ¿la aplicación falla y se detiene?
Intento solucionarlo, pero no conozco el problema, ¿qué debo hacer?
aquí el ex
Newtonsoft.Json.JsonSerializationException
Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CCSN.Models.Patient]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal.NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '0', line 1, position 5.
en esta fila para tener paciencia
return JsonConvert.DeserializeObject<TEntity>(json);
public static async Task<TEntity> Get<TEntity>(string url)
{
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
HttpClient client = new HttpClient(clientHandler);
var response = await client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TEntity>(json);
}
es el error de no convertir?
y ¿cómo puedo convertir a objeto o json?
JSON
{"0":{"Citas":[{"AppointmentDate":"2022-04-12T00:00:00+03:00","AppointmentPatientName":"Ghaidaa","AppointmentTime":"00:00:00 ","FollowUpAddNote":"prueba","FollowUpDate":"22-2-2022","FollowUpGoals":"R alpha","FollowUpTools":"Ninguno","ID":"0","PatientID":"0"},{"AppointmentDate":"2022-04-11T18:58:03.623061+03:00","AppointmentPatientName":"Ghaidaa","FollowUpAddNote":"Ninguno","FollowUpDate":"22- 2-2022","FollowUpGoals":"R alfa","FollowUpTools":"Ninguno","ID":"1","PatientID":"0"},{"Fecha de cita":"2022-04-11T18:58:03.623061+03:00","Nombre de paciente de cita":"Ghaidaa","FollowUpAddNote":"Ninguno","FollowUpDate":"22-2-2022","FollowUpGoals":"R alfa","FollowUpTools":"Ninguno","ID":"2","PatientID":"0"},{"AppointmentDate":"2022-04-11T18:58:03.623061+03:00 ","AppointmentPatientName":"Ghaidaa","FollowUpAddNote":"Ninguno","FollowUpDate":"22-2-2022","FollowUpGoals":"R alpha","FollowUpTools":"Ninguno","ID":"3","IDPaciente":"0"},{"FechaCita":"2022-04-12T00:00:00+03:00","NombrePacienteCita":"Ghaidaa","AppointmentTime":"00:00:00","FollowUpAddNote":"prueba","FollowUpDate":"22-2-2022","FollowUpGoals":"R alpha","FollowUpTools":"Ninguno ","ID":"4","IDPaciente":"0"}],"ID":"0","DirecciónPaciente":"Nablus-Tell","CumpleañosPaciente":"8-3-2001", "PatientGender":"Female","PatientGenticsDiseses":"Ninguno","PatientHeight":168,"PatientMobileNo":"0568982001","PatientName":"Ghaidaa","PatientWeight":60},"1":{ "Citas":[{"FechaCita":"2022-04-12T18:58:03.623061+03:00","NombrePacienteCita":"Ghaidaa","FollowUpAddNote":"Ninguno","FollowUpDate":"22-2-2022","FollowUpGoals":"R alpha","FollowUpTools":"Ninguno","ID":"0","PatientID": "1"},{"AppointmentDate":"2022-04-11T18:58:03.623061+03:00","ApointmentPatientName":"Ghaidaa","FollowUpAddNote":"Ninguno","FollowUpDate":"22-2 -2022","FollowUpGoals":"R alpha","FollowUpTools":"Ninguno","ID":"1","PatientID":"0"},{"AppointmentDate":"2022-04-11T18: 58:03.623061+03:00","CitaPacienteNombre":"Ghaidaa","FollowUpAddNote":"Ninguno","FollowUpDate":"22-2-2022","FollowUpGoals":"R alpha","FollowUpTools":"Ninguno","ID":"2","PatientID":"0"},{"AppointmentDate":"2022-04-11T18:58:03.623061+03:00","AppointmentPatientName":"Ghaidaa","FollowUpAddNote":"Ninguno","FollowUpDate":"22-2-2022","FollowUpGoals":"R alpha","FollowUpTools":"Ninguno"," ID":"3","PatientID":"0"},{"AppointmentDate":"2022-04-12T18:58:03.623061+03:00","AppointmentPatientName":"Ghaidaa","FollowUpAddNote":" Ninguno","FollowUpDate":"22-2-2022","FollowUpGoals":"R alpha","FollowUpTools":"Ninguno","ID":"4","IDPaciente":"0"}],"ID":"1","DirecciónPaciente":"Nablus-Tell","CumpleañosPaciente":"3-8-2001","GéneroPaciente":"Mujer","PatientGenticsDiseses":"Ninguno","PatientHeight":168,"PatientMobileNo":"0568982001","PatientName":"shada","PatientWeight":60},"-N-tZ6hbyPpWOWeoo9o4":{"ID": "45455184","PatientAddress":"g","PatientGender":"h","PatientGenticesDiseses":"b","PatientHeight":"26","PatientMobileNO":"hshhs","PatientName":"sgagaga ","PesoPaciente":"67"}}1","Dirección del paciente":"Nablus-Tell","Cumpleaños del paciente":"3-8-2001","Género del paciente":"Mujer","Enfermedades genéticas del paciente":"Ninguna","Altura del paciente":168,"Móvil del pacienteNo":"0568982001","PatientName":"shada","PatientWeight":60},"-N-tZ6hbyPpWOWeoo9o4":{"ID":"45455184","PatientAddress":"g","PatientGender":"h ","PatientGenticesDiseses":"b","PatientHeight":"26","PatientMobileNO":"hshhs","PatientName":"sgagaga","PatientWeight":"67"}}1","Dirección del paciente":"Nablus-Tell","Cumpleaños del paciente":"3-8-2001","Género del paciente":"Mujer","Enfermedades genéticas del paciente":"Ninguna","Altura del paciente":168,"Móvil del pacienteNo":"0568982001","PatientName":"shada","PatientWeight":60},"-N-tZ6hbyPpWOWeoo9o4":{"ID":"45455184","PatientAddress":"g","PatientGender":"h ","PatientGenticesDiseses":"b","PatientHeight":"26","PatientMobileNO":"hshhs","PatientName":"sgagaga","PatientWeight":"67"}}SexoPaciente":"Female","PatientGenticsDiseses":"Ninguno","PatientHeight":168,"PatientMobileNo":"0568982001","PatientName":"shada","PatientWeight":60},"-N-tZ6hbyPpWOWeoo9o4":{"ID":"45455184","PatientAddress":"g","PatientGender":"h","PatientGenticesDiseses":"b","PatientHeight":"26","PatientMobileNO":"hshhs", "PatientName":"sgagaga","PatientWeight":"67"}}SexoPaciente":"Female","PatientGenticsDiseses":"Ninguno","PatientHeight":168,"PatientMobileNo":"0568982001","PatientName":"shada","PatientWeight":60},"-N-tZ6hbyPpWOWeoo9o4":{"ID":"45455184","PatientAddress":"g","PatientGender":"h","PatientGenticesDiseses":"b","PatientHeight":"26","PatientMobileNO":"hshhs", "PatientName":"sgagaga","PatientWeight":"67"}}-N-tZ6hbyPpWOWeoo9o4":{"ID":"45455184","PatientAddress":"g","PatientGender":"h","PatientGenticesDiseses":"b","PatientHeight":"26","PatientMobileNO":"hshhs","PatientName":"sgagaga","PatientWeight":"67"}}-N-tZ6hbyPpWOWeoo9o4":{"ID":"45455184","PatientAddress":"g","PatientGender":"h","PatientGenticesDiseses":"b","PatientHeight":"26","PatientMobileNO":"hshhs","PatientName":"sgagaga","PatientWeight":"67"}}
Solución del problema
El ir y venir en los comentarios se ha vuelto difícil de manejar. Aquí hay una wiki de la comunidad que muestra el estado de resolución de esto.
Excepción:
Newtonsoft.Json.JsonSerializationException
Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CCSN.Models.Patient]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal.NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '0', line 1, position 5.
NOTA: La excepción dice que está tratando de deserializar en
'System.Collections.Generic.List`1[CCSN.Models.Patient]'
En C# eso sería un List<Patient>
.
Declaración en PatientServices.Addpat en la que se cree que ocurre la excepción:
var x = await firebaseClient
.Child($"Specalists/406707265/Patients")
.PostAsync(new Patient(patient));
Esas declaraciones deben conducir a esto: la línea de nivel inferior en la que se produce la excepción:
return JsonConvert.DeserializeObject<TEntity>(json);
@nombre:
json
. En su valor, la pequeña flecha hacia abajo le permite seleccionar cómo verlo. Use TextVisualizer (o si hay una opción Json, úsela). Copie el valor de Visualizer y péguelo aquí:pegue el valor json aquí
TEntity
esa línea, ¿VS muestra un tipo?Tipo de entidad aquí
Addpat
dentro de ella.pegar pila aquí
No hay comentarios:
Publicar un comentario