如何发布数组json .net?

时间:2019-04-04 11:25:04

标签: c# json

我正在插入json格式,但需要知道如何按以下所述以这种格式插入

{
    "products": [
        {
            "product_id": "55",
            "name": "testing data",
            "price": "77",
            "total": "77",
            "quantity": "1",
            "type": "kg"
        }],


 ],
    "totals": [
        {
            "code": "sub_total",
            "title": "Sub-Total",
            "text": "Rs277.00",
            "value": "277.0000",
            "sort_order": "1"
        }]
    }

这是我正在尝试的代码

items local = new items();
foreach (var item in _LocalItem){

    local = new items
    {
        name = item.name
    };
}

var json = JsonConvert.SerializeObject(local);

var request = new HttpRequestMessage(HttpMethod.Post, "http://orangepotato.rjcpacking.com/index.php?route=api/login/addcustomerOrder");
request.Content = new StringContent(json);

我不明白我可以在哪里添加json格式的“产品”数组

1 个答案:

答案 0 :(得分:0)

简单来说:不要序列化数组-序列化在一个名为products的成员中具有的东西:

var json = JsonConvert.SerializeObject(new { products = local });