Chris
Chris

Reputation: 51

How create array title in json FROM SQL?

Currently, my result is like this:

[    
      {  
         "ib_itemcode1":"0 ",
         "transtatuscode":"IN",
         "invtid":"02007997 ",
         "descr":"Pantene C/C Intensive Care Mask 6 x 150m "
      },
      {  
         "ib_itemcode1":"12 ",
         "transtatuscode":"12",
         "invtid":"12 ",
         "descr":"12 "
      },
      {  
         "ib_itemcode1":"1232131 ",
         "transtatuscode":"ss",
         "invtid":"123123 ",
         "descr":"qweqweqwe "
      },
      {  
         "ib_itemcode1":"13 ",
         "transtatuscode":"13",
         "invtid":"13 ",
         "descr":"13 "
      },
      {  
         "ib_itemcode1":"47400179172 ",
         "transtatuscode":"IN",
         "invtid":"13101336 ",
         "descr":"Gillette Mach 3 Dispenser 8S (X12) "
      },
      {  
         "ib_itemcode1":"47400179349 ",
         "transtatuscode":"IN",
         "invtid":"13101473 ",
         "descr":"Gillette Mach3 Cart 4S (X12) "
      }
]

how do I set a product title so that it will look something like this

[  
 "Product":[
   {  
      "ib_itemcode1":"0 ",
      "transtatuscode":"IN",
      "invtid":"02007997 ",
      "descr":"Pantene C/C Intensive Care Mask 6 x 150m "
   },
   {  
      "ib_itemcode1":"12 ",
      "transtatuscode":"12",
      "invtid":"12 ",
      "descr":"12 "
   },
   {  
      "ib_itemcode1":"1232131 ",
      "transtatuscode":"ss",
      "invtid":"123123 ",
      "descr":"qweqweqwe "
   },
   {  
      "ib_itemcode1":"13 ",
      "transtatuscode":"13",
      "invtid":"13 ",
      "descr":"13 "
   },
   {  
      "ib_itemcode1":"47400179172 ",
      "transtatuscode":"IN",
      "invtid":"13101336 ",
      "descr":"Gillette Mach 3 Dispenser 8S (X12) "
   },
   {  
      "ib_itemcode1":"47400179349 ",
      "transtatuscode":"IN",
      "invtid":"13101473 ",
      "descr":"Gillette Mach3 Cart 4S (X12) "
   }
           ]
]

The way I pull all these is from SQL server.

I had two projects in a Solution for visual studio.

  1. ProductDataAccess (database)

  2. ProcuctServiceFinal (The codes to create Rest services)

In (1)

namespace ProductDataAccess
{
using System;
using System.Collections.Generic;

public partial class product
{
    public string ib_itemcode1 { get; set; }
    public string transtatuscode { get; set; }
    public string invtid { get; set; }
    public string descr { get; set; }
}
}

Where or how should I go or add the title?

Upvotes: 0

Views: 52

Answers (1)

Sefe
Sefe

Reputation: 14017

The first output is not correct JSON, as an array has no properties. You probably mean { "Product": [...] } instead of [ "Product": [...] ]. With curly braces instead of square bracket it is the serialization output of an object of this class:

class MyJsonClass {
    public product[] Product { get; set; }
}

Which you would instantiate like this (assuming you have the variables product1 to product6 with the desired content):

MyJsonClass itemToSerialize = new MyJsonClass() {
    Product = { product1, product2, product3, product4, product5, product6 }
};

If you want an output like your second exaple you need to serialize an array of product:

product[] itemToSerialize = { product1, product2, product3, product4, product5, product6 };

Upvotes: 2

Related Questions