我正在使用Xamarin Forms在C#中进行编码,并且只是试图从JSON响应中提取一些数据。
我正在尝试反序列化JSON响应,但似乎无法拉出嵌套的变量(计费和运输)
这是JSON响应:
[{
"id": xxxx,
"date_created": "2020-09-04T11:01:46",
"date_created_gmt": "2020-09-04T01:01:46",
"date_modified": "2020-09-04T11:06:42",
"date_modified_gmt": "2020-09-04T01:06:42",
"email": "xxxx",
"first_name": "xxx",
"last_name": "xxx",
"role": "customer",
"username": "xxxxx",
"billing": {
"first_name": "xxx",
"last_name": "xxx",
"company": "xxx xxx",
"address_1": "XXX",
"address_2": "XXXX",
"city": "XXX",
"postcode": "XXX",
"country": "XXX",
"state": "XXX",
"email": "XXXX",
"phone": "xxxx"
},
"shipping": {
"first_name": "xxx",
"last_name": "xx",
"company": "xxx",
"address_1": "xxx",
"address_2": "xxxx",
"city": "xxx",
"postcode": "xxx",
"country": "xx",
"state": "xxx"
},
"is_paying_customer": false,
这是我的代码:
root2 myDeserializedClass = JsonConvert.DeserializeObject<Root2>(response_package);
Console.WriteLine("Shipping First Name: " + myDeserializedClass.MyArray[0].billing.first_name);
这是我的课程(从json2csharp生成)
public class Billing
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string postcode { get; set; }
public string country { get; set; }
public string state { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
public class Shipping
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string postcode { get; set; }
public string country { get; set; }
public string state { get; set; }
}
public class MyArray
{
public int id { get; set; }
public DateTime date_created { get; set; }
public DateTime date_created_gmt { get; set; }
public DateTime date_modified { get; set; }
public DateTime date_modified_gmt { get; set; }
public string email { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string role { get; set; }
public string username { get; set; }
public Billing billing { get; set; }
public Shipping shipping { get; set; }
public bool is_paying_customer { get; set; }
public string avatar_url { get; set; }
public List<MetaData> meta_data { get; set; }
public Links _links { get; set; }
}
public class Root2
{
public List<MyArray> MyArray { get; set; }
}
我使用Root2的原因是因为正在使用另一个Root(无双关语)
尝试这个:
var lst = JsonConvert.DeserializeObject<MyArray[]>(response_package);
Console.WriteLine("Shipping First Name: " + lst[0].billing.first_name);
JSON中没有具有此签名的内容:
{
MyArray = [{ ... },{ ... }]
}
它具有以下签名:
[{ ... },{ ... }]
由于基础对象是数组,因此必须反序列化为数组类型。List<MyArray>
也可以在这里工作,例如:
var lst = JsonConvert.DeserializeObject<List<MyArray>>(response_package);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句