0

我有一些从 API 返回的 JSON,如下所示,我需要将其解析为其他一些可用对象的数组:

{
    "Patients": [
        {
            "ChartNumber": "U1EQ643",
            "DateOfBirth": {
                "Raw": "19940704",
                "Value": null
            },
            "FirstName": "Joe",
            "LastName": "Smith",
            "MiddleName": null,
            "Sex": "M",
            "SocialSecurityNumber": {
                "Formatted": "xxx-xx-xxxx",
                "Raw": "xxxxxxxxx"
            }
        },
        {
            "ChartNumber": "abcQ643",
            "DateOfBirth": {
                "Raw": "19910614",
                "Value": null
            },
            "FirstName": "Alison",
            "LastName": "Smith",
            "MiddleName": null,
            "Sex": "F",
            "SocialSecurityNumber": {
                "Formatted": "xxx-xx-xxxx",
                "Raw": "xxxxxxxxx"
            }
        },
}

到目前为止,这是我的代码中的内容:

class Svc1
{
    public void TrySearch2()
    {
        string URL = "https://SOMEWHERE.com/search";

        WebRequest request = WebRequest.Create(URL);

        request.Method = "POST";
        request.ContentType = "application/json; charset=utf-8";

        string searchFor = "smith";
        string postData = "{\"Authentication\": {\"SubscriberKey\": \"ABC\"," +
                                                "\"SiteServiceKey\": \"DEF\"}," +
                                                "\"" + requestType + "\": \"" + searchFor + "\"}";

        //get a reference to the request-stream, and write the postData to it
        using (Stream s = request.GetRequestStream())
        {
            using (StreamWriter sw = new StreamWriter(s))
                sw.Write(postData);
        }

        //get response-stream, and use a streamReader to read the content
        using (Stream s = request.GetResponse().GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(s))
            {
                var jsonData = sr.ReadToEnd();

                var ds = new DataContractJsonSerializer(typeof(Patient[]));
                var msnew = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
                Patient[] items = (Patient[])ds.ReadObject(msnew);

            }
        }

    }

//patient class
[Serializable]
public class Patient
{
    private string _chartNumber;
    private string _dateOfBirth;
    private string _firstName;
    private string _lastName;
    private string _middleName;
    private string _sex;
    private string _ssNum;

    public Patient(string chartNo, string DOB, string first, string last, string middle, string sex, string SSNum)
    {
        this._chartNumber = chartNo;
        this._dateOfBirth = DOB;
        this._firstName = first;
        this._lastName = last;
        this._middleName = middle;
        this._sex = sex;
        this._ssNum = SSNum;
    }
    public string chartNumber
    {
        get { return _chartNumber; }
        set { _chartNumber = value; }
    }
    public string dateOfBirth
    {
        get { return _dateOfBirth; }
        set { _dateOfBirth = value; }
    }
    public string firstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }
    public string lastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }
    public string middleName
    {
        get { return _middleName; }
        set { _middleName = value; }
    }
    public string sex
    {
        get { return _sex; }
        set { _sex = value; }
    }
    public string SSNumber
    {
        get { return _ssNum; }
        set { _ssNum = value; }
    }
}
4

2 回答 2

1

从您提供的 Json 中,我认为您需要一个包含属性 Patients 的类

public class ListOfPatients
{
    public ListOfPatinets(){Patients = new List<Patinent>();}

    public List<Patient> Patients{get;set;}
}

然后从您的 Json 反序列化该新类的对象

  var ds = new DataContractJsonSerializer(typeof(ListOfPatients));
  var msnew = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
  ListOfPatients list = (ListOfPatients)ds.ReadObject(msnew);
  var patients = list.Patients;
  .....

而且我相信您需要在 Patient 类中使用默认构造函数,并且不确定 DataContractSerializer 在将 Json 与对象属性匹配时是否区分大小写。在 Json 中有“ChartNumber”,在 Patient 类中有“chartNumber”,这可能会导致问题,但我不确定。

于 2013-04-23T19:45:10.733 回答
1

请将 DataMember 属性放在 Parent 类的每个属性上。

还有一件事,您定义的类结构与您提到的 json 不同。

dateofBirth 和 SocialSecurityNumbers 也是对象,您将其视为 Parent 类中的字符串。

于 2013-04-23T19:54:01.780 回答