2013-07-05 72 views
4

我有一個JSON字符串像這樣,,我瓦納負載在C#陣列。當我試圖做到這一點我得到異常如何在C#中獲取Json數組?

我的字符串:

{ 
"customerInformation": 
[ 
    { 
    "customerId":"123", 
    "CustomerName":"", 
    "Age":39, 
    "Gender":"Male", 
    "StudyInfo":[ 
     { 
      "Modality":"XRAY", 
      "StudyName":"Test Name", 
      "ModalityId":"1", 
      "StudyID":"10923", 
      "visitid":41549113, 
      "billingId":"456", 
      "RegDate":"mm/dd/yyyy", 
      "uploaded":"1", 
      "groupid":"1" 

     }, 
     { 
      "Modality":"XRAY", 
      "StudyName":"CT Test Name", 
      "ModalityId":"1", 
      "StudyID":"10924", 
      "visitid":41549113, 
      "billingId":"459", 
      "RegDate":"mm/dd/yyyy", 
      "uploaded":"1", 
      "groupid":"1" 

     } 
    ] 
    }, 

    { 
    "customerId":"928", 
    "CustomerName":"", 
    "Age":49, 
    "Gender":"FeMale", 
    "StudyInfo":[ 
     { 
      "Modality":"XRAY", 
      "StudyName":"Test Name", 
      "ModalityId":"1", 
      "StudyID":"10923", 
      "visitid":41549113, 
      "billingId":"456", 
      "RegDate":"mm/dd/yyyy", 
      "uploaded":"1", 
      "groupid":"1" 
     }, 
     { 
      "Modality":"XRAY", 
      "StudyName":"CT Test Name", 
      "ModalityId":"1", 
      "StudyID":"10924", 
      "visitid":41549113, 
      "billingId":"459", 
      "RegDate":"mm/dd/yyyy", 
      "uploaded":"1", 
      "groupid":"1" 
     } 
    ] 
    } 

] 

} 

我的代碼:

public class Attributes 
{ 


    public string[] customerId { get; set; } 
    public string[] CustomerName { get; set; } 
    public string[] Age { get; set; } 
    public string[] Gender { get; set; } 
    public string[] StudyInfo { get; set; } 
    public string[] Modality { get; set; } 
    public string[] StudyName { get; set; } 
    public string[] ModalityId { get; set; } 
    public string[] StudyID { get; set; } 
    public string[] visitid { get; set; } 
    public string[] billingId { get; set; } 
    public string[] RegDate { get; set; } 
    public string[] uploaded { get; set; } 
} 

public class DataJsonAttributeContainer 
{ 
    public List<Attributes> attributes { get; set; } 
} 

public static T DeserializeFromJson<T>(string json) 
    { 
     T deserializedProduct = JsonConvert.DeserializeObject<T>(json); 
     return deserializedProduct; 
    } 

    public void testing() 
    { 
    var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonString); 

    } 

「返回null」

我已經試過這也

  JArray jArray = (JArray)JsonConvert.DeserializeObject(JsonStr); 
      dynamic dynObj1 = jArray.OrderByDescending(x => x["customerId"]); 

這兩種情況下得到了失敗...如何加載這個..我使用Newtonsoft.Json的Dll

+0

先嚐試一個較小的樣本,然後繼續工作。有些提示一個很長的路,就是你要轉換的對象應該與json具有相同的結構,而不是在你的示例代碼中。 –

+0

在你的json中,該數組被稱爲'customerInformation',但在你的容器類中被稱爲'attributes'。嘗試在兩者中使用相同的名稱。 – alun

回答

1

user2552410!

也許你需要改變你的班級結構。您可以使用清單<>

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using ConsoleApplication2.TestService; 
using Newtonsoft.Json; 

namespace ConsoleApplication2 
{ 
    public class Customer 
    { 
     public string customerId { get; set; } 
     public string CustomerName { get; set; } 
     public string Age { get; set; } 
     public string Gender { get; set; } 
     public StudyInfoType[] StudyInfo { get; set; } 
     public string visited { get; set; } 
     public string billingId { get; set; } 
     public string RegDate { get; set; } 
     public string uploaded { get; set; } 
    } 

    public class StudyInfoType 
    { 
      string Modality {get; set;} 
      string StudyName {get; set;} 
      string ModalityId {get; set;} 
      string StudyID {get; set;} 
      string visitid {get; set;} 
      string billingId {get; set;} 
      string RegDate {get; set;} 
      string uploaded {get; set;} 
      string groupid { get; set; } 
    } 


    class Program 
    { 
     static void Main() 
     { 
      var temp = CustomerInfo(@"[{ 'customerId':'123', 'CustomerName':'', 'Age':39,'Gender':'Male','StudyInfo':[{'Modality':'XRAY','StudyName':'Test Name','ModalityId':'1','StudyID':'10923','visitid':41549113,'billingId':'456','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'},{'Modality':'XRAY','StudyName':'CT Test Name','ModalityId':'1','StudyID':'10924','visitid':41549113,'billingId':'459','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'}]},{'customerId':'928','CustomerName':'','Age':49,'Gender':'FeMale','StudyInfo':[{'Modality':'XRAY','StudyName':'Test Name','ModalityId':'1','StudyID':'10923','visitid':41549113,'billingId':'456','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'},{ 'Modality':'XRAY','StudyName':'CT Test Name','ModalityId':'1','StudyID':'10924','visitid':41549113,'billingId':'459','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1' } ] } ]"); 
     } 

     public static List<Customer> CustomerInfo(string json) 
     { 
      var n = JsonConvert.DeserializeObject(json, new JsonSerializerSettings 
      { 
       ObjectCreationHandling = ObjectCreationHandling.Replace 
      }); 
      return JsonConvert.DeserializeObject<List<Customer>>(json); 
     } 
    } 
} 
+0

但字符串應以「customerInformation」開頭:您直接添加客戶ID – Aravind

+0

是的,它是正確的。在我看來,創建兩個表格會更好。一個用於「CustomerInforms」,一個用於「訪問詳細信息」。 「CustomerInformation」表包含List <訪問詳細信息> – IluhaPuts

7

你生成你對象的方式是錯誤的,它應該是這樣的:

public class StudyInfo 
{ 
    public string Modality { get; set; } 
    public string StudyName { get; set; } 
    public string ModalityId { get; set; } 
    public string StudyID { get; set; } 
    public int visitid { get; set; } 
    public string billingId { get; set; } 
    public string RegDate { get; set; } 
    public string uploaded { get; set; } 
    public string groupid { get; set; } 
} 

public class CustomerInformation 
{ 
    public string customerId { get; set; } 
    public string CustomerName { get; set; } 
    public int Age { get; set; } 
    public string Gender { get; set; } 
    public List<StudyInfo> StudyInfo { get; set; } 
} 

public class RootObject 
{ 
    public List<CustomerInformation> customerInformation { get; set; } 
} 

順便說一句,你可以嘗試json2charp,對於這樣的東西,這是非常棒的。

+0

先生仍然返回屬性爲空... – Aravind