2013-04-23 53 views
0

我有一些JSON從API返回,如下所示,我需要解析一些其他可用對象的數組: 以下是一些更詳細的信息,讓我看後,直到我寫的代碼括號外的一些話需要使用asp.net解析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; } 
     } 
    } 
+0

DataContractJsonSerializer(如您所發佈的代碼中所給出的)將爲您完成這項工作。不知道你在找什麼。 – aquaraga 2013-04-23 18:03:15

+0

您也可以使用動態數據類型。看看這個SO帖子http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – 2013-04-23 18:09:50

+1

嘗試JSON.NET,我用了很多:http://james.newtonking .com/projects/json-net.aspx – 2013-04-23 18:16:27

回答

1

請將DataMember屬性放在您父類的每個屬性上。

還有一件事,你定義的類結構並不像你提到的json。

dateofBirth和SocialSecurityNumbers也是對象,您將它視爲Parent類中的字符串。

+0

是的,我有一位同事在我發佈後不久告訴我。一切都很好。我用JSON同步了類結構,並且與世界一切都很好...... – 2013-04-24 20:13:08

1

從您提供的JSON,我想你需要一個 類,它包含屬性患者

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

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

,然後從該新類

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到對象屬性。在Json中你有「ChartNumber」,在Patient類中你有「chartNumber」,這可能會導致問題,但我不知道。

+0

感謝'jure'和'sohail.hussain.dyn',希望我能用綠色複選標記標記你們兩個。 – 2013-04-24 20:14:35