从列表中获取值(C#UWP)
我有课从列表中获取值(C#UWP),c#,uwp,C#,Uwp,我有课 public class RootObject { public int id { get; set; } public int parent_id { get; set; } public string status { get; set; } public string order_key { get; set; } public string currency { get; set; }
public class RootObject
{
public int id { get; set; }
public int parent_id { get; set; }
public string status { get; set; }
public string order_key { get; set; }
public string currency { get; set; }
public string version { get; set; }
public bool prices_include_tax { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int customer_id { get; set; }
public double discount_total { get; set; }
public double discount_tax { get; set; }
public double shipping_total { get; set; }
public double shipping_tax { get; set; }
public double cart_tax { get; set; }
public double total { get; set; }
public double total_tax { get; set; }
public Billing billing { get; set; }
public Shipping shipping { get; set; }
public string payment_method { get; set; }
public string payment_method_title { get; set; }
public string transaction_id { get; set; }
public string customer_ip_address { get; set; }
public string customer_user_agent { get; set; }
public string created_via { get; set; }
public string customer_note { get; set; }
public string date_completed { get; set; }
public string date_paid { get; set; }
public string cart_hash { get; set; }
public List<object> line_items { get; set; }
public List<object> tax_lines { get; set; }
public List<object> shipping_lines { get; set; }
public List<object> fee_lines { get; set; }
public List<object> coupon_lines { get; set; }
}
],
如何获取例如产品\u id
非常感谢你的帮助 如果不够明显
using Newtonsoft.Json.Linq;
foreach(object line_item in line_items)
{
JObject line_item_jobject = line_item as JObject;
Jvalue product_id = line_item_jobject["product_id"] as JValue;
int product_id_value = (int)product_id.Value;
// Another way
int product_id_value2 = line_item_jobject.Value<int>("product_id");
}
使用Newtonsoft.Json.Linq;
foreach(行项目中的对象行项目)
{
JObject行项目JObject=行项目作为JObject;
Jvalue product_id=行项目jobject[“product_id”]作为Jvalue;
int product_id_value=(int)product_id.value;
//另一种方式
int product_id_value2=行项目项目价值(“product_id”);
}
您可以使用非常强大的动态:
List<dynamic> lineItems = jsonObject.line_items;
foreach (dynamic line_item in lineItems)
{
int productId = line_item.product_id;
}
List lineItems=jsonObject.line\u项目;
foreach(行项目中的动态行项目)
{
int productId=行\u项.product\u id;
}
记住:
它可能因重命名/其他因素而崩溃-
dynamic
功能非常强大,但使用时要小心
您也可以直接访问它,而不需要您创建的“类”-只需反序列化为动态并访问字段 你有搜索条件吗?如果是这样,您可以使用linq表达式搜索列表,然后使用
Object.value
Yes提取值,我知道需要哪些字段。如何搜索和提取@Takarii快速查看,看看它是否符合您的需要。我有JSON、类和反序列化。我需要去列表并从中提取我需要的数据。就像我用id
I编辑的smth一样post@Takariiso,使用我发布的链接,使用linq构造查询以获取包含该值的对象,然后直接访问该值。
using Newtonsoft.Json.Linq;
foreach(object line_item in line_items)
{
JObject line_item_jobject = line_item as JObject;
Jvalue product_id = line_item_jobject["product_id"] as JValue;
int product_id_value = (int)product_id.Value;
// Another way
int product_id_value2 = line_item_jobject.Value<int>("product_id");
}
List<dynamic> lineItems = jsonObject.line_items;
foreach (dynamic line_item in lineItems)
{
int productId = line_item.product_id;
}