0

i am inserting json format but need to know how i can insert in this format as mention below

{
    "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"
        }]
    }

here is my code which i am trying

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);

i dont understand where i can add "products" array in json format

waqas waqas
  • 159
  • 5
  • 17
  • 1
    that `foreach` look looks very suspicious... it is discarding whatever you put into `local` – Marc Gravell Apr 04 '19 at 11:29
  • Possible duplicate of [How to post JSON to a server using C#?](https://stackoverflow.com/questions/9145667/how-to-post-json-to-a-server-using-c) – Tom W Apr 04 '19 at 11:32

1 Answers1

0

Simply: don't serialize an array - serialize something that has an array in a member called products:

var json = JsonConvert.SerializeObject(new { products = local });
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784