2017-06-10 30 views
0

我想動態解析JSON數據並讀取它的鍵和它們的值,而無需將它序列化到具體的類中,因爲JSON數據可能會有不同的時間。但是對於最初的測試,一個樣本數據就是這樣的。在這個數據中,我想獲取屬性值,如果JSON數據也包含複雜的結構,我也想讀取子屬性值如何在C#中做到這一點。解析JSON數據並讀取其屬性值

{"id":4877891717,"email":"[email protected]","accepts_marketing":true,"created_at":"2017-03-24T08:39:56+01:00","updated_at":"2017-04-10T09:40:42+02:00","first_name":"Robin","last_name":"Van Persie","orders_count":8,"state":"disabled","total_spent":"2320.00","last_order_id":4434634693,"note":"","verified_email":true,"multipass_identifier":null,"tax_exempt":true,"phone":"+3225551212","tags":"","last_order_name":"#1116","addresses":[{"id":5143111941,"first_name":"Robin","last_name":"Van Persie","company":"InSync","address1":"CB 28, El Solo Road","address2":"CB 28, El Solo Road","city":"Brussels","province":"EU","country":"Belgium","zip":"123456","phone":"12345678","name":"Robin Van Persie","province_code":null,"country_code":"BE","country_name":"Belgium","default":true}],"default_address":{"id":5143111941,"first_name":"Robin","last_name":"Van Persie","company":"InSync","address1":"CB 28, El Solo Road","address2":"CB 28, El Solo Road","city":"Brussels","province":"EU","country":"Belgium","zip":"123456","phone":"12345678","name":"Robin Van Persie","province_code":null,"country_code":"BE","country_name":"Belgium","default":true}} 

我正在嘗試這種方式。但是在接下來我會做什麼。

foreach (JObject token in jObject.Children()) 
      {} 

感謝

+0

你試圖使用索引,而不是關鍵...? –

回答

0

你有兩個選擇(據我所知)。你可以根據你的json字符串生成一個類,然後解析結果。像這樣:

public class Address 
{ 
    public long id { get; set; } 
    public string first_name { get; set; } 
    public string last_name { get; set; } 
    public string company { get; set; } 
    public string address1 { get; set; } 
    public string address2 { get; set; } 
    public string city { get; set; } 
    public string province { get; set; } 
    public string country { get; set; } 
    public string zip { get; set; } 
    public string phone { get; set; } 
    public string name { get; set; } 
    public object province_code { get; set; } 
    public string country_code { get; set; } 
    public string country_name { get; set; } 
    public bool @default { get; set; } 
} 

public class DefaultAddress 
{ 
    public long id { get; set; } 
    public string first_name { get; set; } 
    public string last_name { get; set; } 
    public string company { get; set; } 
    public string address1 { get; set; } 
    public string address2 { get; set; } 
    public string city { get; set; } 
    public string province { get; set; } 
    public string country { get; set; } 
    public string zip { get; set; } 
    public string phone { get; set; } 
    public string name { get; set; } 
    public object province_code { get; set; } 
    public string country_code { get; set; } 
    public string country_name { get; set; } 
    public bool @default { get; set; } 
} 

public class RootObject 
{ 
    public long id { get; set; } 
    public string email { get; set; } 
    public bool accepts_marketing { get; set; } 
    public string created_at { get; set; } 
    public string updated_at { get; set; } 
    public string first_name { get; set; } 
    public string last_name { get; set; } 
    public int orders_count { get; set; } 
    public string state { get; set; } 
    public string total_spent { get; set; } 
    public long last_order_id { get; set; } 
    public string note { get; set; } 
    public bool verified_email { get; set; } 
    public object multipass_identifier { get; set; } 
    public bool tax_exempt { get; set; } 
    public string phone { get; set; } 
    public string tags { get; set; } 
    public string last_order_name { get; set; } 
    public List<Address> addresses { get; set; } 
    public DefaultAddress default_address { get; set; } 
} 

然後分析它(使用json.net)是這樣的:

var jObject=JsonConvert.DeserializeObject<RootObject>(responseString); 

,或者您可以使用dynamic對象和處理在運行時JSON結果。像這樣:

var jObject=JsonConvert.DeserializeObject(responseString); 
foreach (JObject token in jObject.Children) 
+0

我無法爲此任務創建具體類 – Utpal

+0

好。然後使用動態。 – David

1

您可以使用NewtonsoftJson庫來解析數據容易

dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(Jsondata); 
    foreach (var product in x) { 
     Messagebox.show(product.data.ToString()); 
     } 
0

您可以使用NewtonsoftJson庫,而無需創建具體類

dynamic parseJson = JsonConvert.DeserializeObject("your json"); 

解析,並得到使用以下代碼的值如...

string Id=parseJson.Id.Value 

我已經測試它和它的工作對我來說