0

顧客の注文のリストを返すクエリを実行します。

SELECT cust_no, cust_name, order_no, order_name FROM CustomerOrders

私のController Action方法では、次のことを行います ( _contextis my DataContext):

var results = _context.CustomerOrders.ToList();

return Json(results, JsonRequestBehavior.AllowGet);

デバッガーでこれを調べると、オブジェクトのリストが表示されますが、Json に慣れていないため、Json 文字列としてこれがどのように見えるかわかりません。私が望むフォーマットは次のとおりです。

{
    "Customer": {
        "cust_no": "123",
        "cust_name": "john",
        "Orders": [
            {
                "order_no": "1",
                "order_name": "order1"
            },
            {
                "order_no": "2",
                "order_name": "order2"
            }
        ]
     },
     "Customer": {
         "cust_no": "456",
         "cust_name": "jane",
         "Orders": [
            {
                "order_no": "3",
                "order_name": "order3"
            },
            {
                "order_no": "4",
                "order_name": "order4"
            }
        ]
    }
}

私は現在これに入ることができます:

{ Customer = "123", cust_name = "John", Orders = "1", oder_no = "order1" }

と:

_context.CustomerOrders.Select(x => new 
                                    {
                                        Customer= x.cust_no, x.cust_name, 
                                        Orders = x.order_no, x.order_name});

public ActionResult GetCustomerOrders()
    {
        JsonResult result = null;
        try
        {
            var results = new {Customers = _context.CustomerOrders.ToList()};

            return Json(results,JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            //Log
        }

        return result;
    }
4

1 に答える 1

2

同じネスト レベル (この場合はレベル 0) で同じキーを持つ JSON キーと値のペアを使用することはできません。必要に応じて、次のようにして匿名型を返すことができます。

var results = new {Customers = _context.CustomerOrders.ToList()};

- 上記のコードが次のようになることを追加するのを忘れていました。

{
    Customers: [{
        "cust_no": "123",
            "cust_name": "john",
            "Orders": [{
            "order_no": "1",
                "order_name": "order1"
        }, {
            "order_no": "2",
                "order_name": "order2"
        }]
    }, {
        "cust_no": "456",
            "cust_name": "jane",
            "Orders": [{
            "order_no": "3",
                "order_name": "order3"
        }, {
            "order_no": "4",
                "order_name": "order4"
        }]
    }]
}

ここで JSON の仕様を確認してください: http://www.json.org/

編集 - (Linq クエリの修正に応じて) 必要なものは次のとおりです。

    var results = (from co in _context.CustomerOrders
                   group co by new { co.cust_no, co.cust_name } into orders
                   select new
                   {
                       Customer = new
                       {
                           cust_name = orders.Key.cust_name,
                           cust_no = orders.Key.cust_no,
                           Orders = orders.Select(o=> new{o.order_name,o.order_no })
                       }
                   }).AsEnumerable();
    return Json(results, JsonRequestBehavior.AllowGet);

モック データを使用した結果は、次のようになります。

[{
    "Customer": {
        "cust_name": "ted",
        "cust_no": "1441865486",
        "Orders": [{
            "order_name": "ted1271196444",
            "order_no": "1877898370"
        }, {
            "order_name": "ted1137404580",
            "order_no": "1033969821"
        }, {
            "order_name": "ted113580415",
            "order_no": "844051358"
        }, {
            "order_name": "ted842422359",
            "order_no": "1063097922"
        }, {
            "order_name": "ted2140579126",
            "order_no": "1170215299"
        }, {
            "order_name": "ted843928549",
            "order_no": "2143378901"
        }]
    }
}, {
    "Customer": {
        "cust_name": "Jack",
        "cust_no": "1258770771",
        "Orders": [{
            "order_name": "Jack879867938",
            "order_no": "585569719"
        }, {
            "order_name": "Jack1423388998",
            "order_no": "209013154"
        }]
    }
}, {
    "Customer": {
        "cust_name": "joe",
        "cust_no": "1223478754",
        "Orders": [{
            "order_name": "joe1283306017",
            "order_no": "1305330220"
        }, {
            "order_name": "joe1369830458",
            "order_no": "1996259538"
        }, {
            "order_name": "joe1772918032",
            "order_no": "1265675292"
        }, {
            "order_name": "joe535974281",
            "order_no": "837890619"
        }, {
            "order_name": "joe194556914",
            "order_no": "812224857"
        }, {
            "order_name": "joe28812423",
            "order_no": "515669909"
        }, {
            "order_name": "joe2004245093",
            "order_no": "1634742463"
        }]
    }
}, {
    "Customer": {
        "cust_name": "jill",
        "cust_no": "659748358",
        "Orders": [{
            "order_name": "jill1462582377",
            "order_no": "1817173079"
        }, {
            "order_name": "jill1848627650",
            "order_no": "830495643"
        }, {
            "order_name": "jill727215465",
            "order_no": "728808273"
        }, {
            "order_name": "jill1071911623",
            "order_no": "824043403"
        }, {
            "order_name": "jill126843849",
            "order_no": "1654825240"
        }]
    }
}]
于 2013-06-26T19:55:47.653 に答える