【发布时间】:2016-01-27 19:00:03
【问题描述】:
我是新手 C# 开发人员,并且是 Linq 的新手,但我试图弄清楚如何将嵌套的子对象从 Linq 查询中获取到对象中。我可以得到主/根对象。
这是我目前的代码和我正在使用的 Json 文件。请注意,如果 Json 出现格式错误,我会手动从 Json 中删除一些文本。
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.processJson();
Console.ReadLine();
}
public void processJson()
{
JObject jobject = JObject.Parse(this.getSampleOrderJsonText());
// QUESTION IS IN THIS BLOCK OF CODE
var orders =
from p in jobject["resource"]["items"].Children()["resource"]
select new orderHeader
{
orderNo = (string)p["orderNo"],
status = (string)p["status"]
// How do I fill "lines" and its child object "lines.lineDetails"?
// lines =
};
foreach (orderHeader orderList in orders)
{
Console.WriteLine(orderList.orderNo + " " + orderList.status);
}
}
public class orderHeader
{
public string orderNo { get; set; }
public string status { get; set; }
public List<orderLine> lines { get; set; }
}
public class orderLine
{
public string sku { get; set; }
public int quantity { get; set; }
public List<orderLineDetail> lineDetails { get; set; }
}
public class orderLineDetail
{
public int productId { get; set; }
public string serialNumber { get; set; }
}
public string getSampleOrderJsonText()
{
return "{Entire Json Text}"; // This returns all the JsonText
}
}
这是我的(已编辑)Json:
{
"status": 200,
"message": "Successful",
"resource": {
"offset": 0,
"total": 1,
"previous": null,
"next": null,
"items": [{
"resource": {
"orderNo": "#6255.1",
"lastUpdatedDate": "2016-01-21T17:39:36-08:00",
"status": "completed",
"vendorId": null,
"vendorExternalId": null,
"id": 153357642,
"items": {
"resource": {
"offset": 0,
"total": 3,
"previous": null,
"next": null,
"items": [{
"resource": {
"sku": "796430390315",
"quantity": 2,
"serialNumbers": {
"resourceLocation": null,
"resource": {
"offset": 0,
"total": 2,
"previous": null,
"next": null,
"items": [{
"resourceLocation": null,
"resource": {
"orderId": 153357642,
"productId": 3525462,
"serialNumber": "T8-0139062"
}
}, {
"resourceLocation": null,
"resource": {
"orderId": 153357642,
"productId": 3525462,
"serialNumber": "T8-0139063"
}
}]
}
},
"productId": 3525462,
"orderId": 153357642,
"ordered": 2,
"shipped": 1
}
}, {
"resource": {
"sku": "796430390322",
"quantity": 2,
"commercialInvoiceValue": 0,
"serialNumbers": {
"resourceLocation": null,
"resource": {
"offset": 0,
"total": 2,
"previous": null,
"next": null,
"items": [{
"resourceLocation": null,
"resource": {
"orderId": 153357642,
"productId": 3525472,
"serialNumber": "T8-0140454"
}
}, {
"resourceLocation": null,
"resource": {
"orderId": 153357642,
"productId": 3525472,
"serialNumber": "T8-0140478"
}
}]
}
},
"productId": 3525472,
"orderId": 153357642,
"ordered": 2,
"shipped": 1
}
}, {
"resourceLocation": null,
"resource": {
"sku": "796430390346",
"quantity": 1,
"commercialInvoiceValue": 0,
"serialNumbers": {
"resourceLocation": null,
"resource": {
"offset": 0,
"total": 1,
"previous": null,
"next": null,
"items": [{
"resourceLocation": null,
"resource": {
"orderId": 153357642,
"productId": 3525482,
"serialNumber": "T8-0141520"
}
}]
}
},
"productId": 3525482,
"orderId": 153357642,
"ordered": 1,
"shipped": 1
}
}]
}
}
"options": {
"resourceLocation": null,
"resource": {
"warehouseId": 13,
"warehouseRegion": "CHI",
"carrierCode": "FDX"
}
}
"shipTo": {
"resource": {
"email": "none@nowhere.com",
"name": "First Last",
"company": "Company, Inc",
"address1": "123 South Street",
"address2": "",
"address3": "",
"city": "Chicago",
"state": "IL",
"postalCode": "60652",
"country": "US",
"phone": "5555551234"
}
}
"pricing": {
"resourceLocation": null,
"resource": {
"shipping": 20.76,
"packaging": 0.88,
"insurance": 9,
"handling": 5.25,
"total": 35.89
}
}
}
}]
}
}
编辑: 工作代码!这是最好的方法吗?如果子元素不存在怎么办?
public void processJson()
{
JObject jobject = JObject.Parse(this.getSampleOrderJsonText());
var orders =
from p in jobject["resource"]["items"].Children()["resource"]
select new orderHeader
{
orderNo = (string)p["orderNo"],
status = (string)p["status"],
lines = (
from nestedChildren in p["items"]["resource"]["items"].Children()["resource"]
select new orderLine
{
sku = (string)nestedChildren["sku"],
quantity = (int)nestedChildren["quantity"]
})
};
foreach (orderHeader orderList in orders)
{
Console.WriteLine(orderList.orderNo + " " + orderList.status);
if (orderList.lines != null)
{
foreach (orderLine line in orderList.lines.ToList())
{
Console.WriteLine("-->{0}: {1}", line.sku, line.quantity);
}
}
}
}
【问题讨论】:
-
为什么不使用嵌套的 linq 查询? lines = from nestedChildren in p ... select ... where
-
我不知道你能做到这一点。我会玩它,看看我能做什么。
-
我刚刚用我认为正确的嵌套选择更新了我的代码,但我收到了一个错误。我觉得我总体上是对的,但我缺少一些简单的东西?
-
我已经成功了!有没有更好的方法来写我的声明?
标签: c# json linq data-structures json.net