返回

c#Newtonsoft.Json.JsonSerializationException Message=无法反序列化当前JSON对象

发布时间:2022-04-20 01:41:38 452
# flask

我尝试将患者添加到firebase数据库。

这是密码

Patient.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 Appointments { get; set; }
    public ObservableCollection 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;

    }
}

PatientServices.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);
}

addPatient.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();
                })
            });
    }
}

addPatient.xaml

       x:Name="PatientAddress">











代码起作用了Patient添加到Firebase,但当它向我发出警报时,应用程序会崩溃并停止?

我知道该怎么做,但我不该怎么做?

这是前任

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) 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.

在这条队伍里,让病人

return JsonConvert.DeserializeObject(json);

  public static async Task Get(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(json);
        }

错误是因为没有转换吗?

我如何转换成object或json

JSON(打印精美)

{
  "0": {
    "Appointments": [
      {
        "AppointmentDate": "2022-04-12T00:00:00+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "AppointmentTime": "00:00:00",
        "FollowUpAddNote": "test",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "0",
        "PatientID": "0"
      },
      {
        "AppointmentDate": "2022-04-11T18:58:03.623061+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "FollowUpAddNote": "None",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "1",
        "PatientID": "0"
      },
      {
        "AppointmentDate": "2022-04-11T18:58:03.623061+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "FollowUpAddNote": "None",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "2",
        "PatientID": "0"
      },
      {
        "AppointmentDate": "2022-04-11T18:58:03.623061+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "FollowUpAddNote": "None",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "3",
        "PatientID": "0"
      },
      {
        "AppointmentDate": "2022-04-12T00:00:00+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "AppointmentTime": "00:00:00",
        "FollowUpAddNote": "test",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "4",
        "PatientID": "0"
      }
    ],
    "ID": "0",
    "PatientAddress": "Nablus-Tell",
    "PatientBirthday": "8-3-2001",
    "PatientGender": "Female",
    "PatientGenticsDiseses": "None",
    "PatientHeight": 168,
    "PatientMobileNo": "0568982001",
    "PatientName": "Ghaidaa",
    "PatientWeight": 60
  },
  "1": {
    "Appointments": [
      {
        "AppointmentDate": "2022-04-12T18:58:03.623061+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "FollowUpAddNote": "None",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "0",
        "PatientID": "1"
      },
      {
        "AppointmentDate": "2022-04-11T18:58:03.623061+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "FollowUpAddNote": "None",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "1",
        "PatientID": "0"
      },
      {
        "AppointmentDate": "2022-04-11T18:58:03.623061+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "FollowUpAddNote": "None",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "2",
        "PatientID": "0"
      },
      {
        "AppointmentDate": "2022-04-11T18:58:03.623061+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "FollowUpAddNote": "None",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "3",
        "PatientID": "0"
      },
      {
        "AppointmentDate": "2022-04-12T18:58:03.623061+03:00",
        "AppointmentPatientName": "Ghaidaa",
        "FollowUpAddNote": "None",
        "FollowUpDate": "22-2-2022",
        "FollowUpGoals": "R alpha",
        "FollowUpTools": "None",
        "ID": "4",
        "PatientID": "0"
      }
    ],
    "ID": "1",
    "PatientAddress": "Nablus-Tell",
    "PatientBirthday": "8-3-2001",
    "PatientGender": "Female",
    "PatientGenticsDiseses": "None",
    "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"
  }
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像