0

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?

2
  • Which programming language is that? Commented Mar 10, 2017 at 11:00
  • That is C#. Judging from the combination of partial classes, automatic properties and .NET namespaces. Commented Mar 10, 2017 at 12:21

1 Answer 1

2

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 };
Sign up to request clarification or add additional context in comments.

1 Comment

hi sir, thanks for the reply. after some one edit my code i think they mess up some part. what i meant was. my json does not have "Product": [...], its just a chunk of [...] how do i add that "Product" <= into my json code so that it looks like what you ment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.