解析JSON数据并读取其属性值

时间:2017-06-10 10:19:29

标签: c# .net json.net

我想动态解析JSON数据并读取其键及其值,而不是在具体类中将其序列化,因为JSON数据可能会不时地进行。但对于初始测试,一个样本数据是这样的。在这个数据中,我想获取属性值,如果JSON数据包含复杂的结构,我也想读取子属性值。如何在C#中执行此操作。

{"id":4877891717,"email":"rvp@insync.com","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())
            {}

谢谢

3 个答案:

答案 0 :(得分:1)

您可以使用NewtonsoftJson库轻松解析数据

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

答案 1 :(得分: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)

答案 2 :(得分:0)

  

您可以使用NewtonsoftJson库进行解析而无需创建具体的类

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

并使用以下代码获取值,如...

string Id=parseJson.Id.Value

我已经测试了它,它正在为我工​​作