VB.net how to serialize list / array of json objects -
i have problem, want serialize list of objects (the objects listed in 'list', , call 'items') json, don't know classes / structures need make following newtonsoft.json serializer:
{ "list": [ { "name": "item1" }, { "name": "item2" }, { "name": "item3" } ], "property1": "value", "property2": "value" }
can know solution problem? lot!
this class nested json serialize.
i think examplen
public class maincatlist public property categorylist() list(of maincategory) end class imports newtonsoft.json imports system imports system.runtime.compilerservices <jsonobject(memberserialization.optin)> public class maincategory <jsonproperty(propertyname:="ana katagori kodu")> public property anakatagorikodu() string <jsonproperty(propertyname:="ana katagori adı")> public property anakatagoriadi() string <jsonproperty(propertyname:="marka")> public property marka() string <jsonproperty(propertyname:="ana Ürün kodu")> public property anaurunkodu() string <jsonproperty(propertyname:="ana Ürün adı")> public property anaurunadi() string <jsonproperty(propertyname:="ana Ürün açıklaması")> public property anaurunaciklamasi() string <jsonproperty(propertyname:="miktar")> public property miktar() integer <jsonproperty(propertyname:="alt kırılımlar")> public property deepcategories list(of deepcategori) public sub new() mybase.new() me.anakatagorikodu = "" me.anakatagoriadi = "" me.marka = "" me.anaurunkodu = "" me.anaurunadi = "" me.anaurunaciklamasi = "" me.miktar = 0 me.deepcategories = new list(of deepcategori)() end sub imports newtonsoft.json imports system imports system.runtime.compilerservices <jsonobject(memberserialization.optin)> public class deepcategori <jsonproperty(propertyname:="katagori kodu")> public property kategorikodu string <jsonproperty(propertyname:="katagori")> public property kategori string <jsonproperty(propertyname:="renk")> public property renk string <jsonproperty(propertyname:="beden")> public property beden string <jsonproperty(propertyname:="barkod")> public property barkod string <jsonproperty(propertyname:="miktar")> public property miktar string <jsonproperty(propertyname:="Ürün kodu")> public property urunkodu string <jsonproperty(propertyname:="Ürün adı")> public property urunadi string <jsonproperty(propertyname:="fiyat")> public property fiyat string public sub new() mybase.new() me.kategorikodu = "" me.kategori = "" me.renk = "" me.beden = "" me.barkod = "" me.miktar = 0 me.urunkodu = "" me.urunadi = "" me.fiyat = 0 end sub end class try dim constr new sqlconnectionstringbuilder constr .datasource = "." .initialcatalog = "yourdb" .userid = "your user id" .password = "your password" end dim ds new datatable using da new sqldataadapter("your sql command", constr.connectionstring) da.fill(ds) dim subds new dataset da.selectcommand.commandtext = "your sql command text" da.fill(subds) dim foundrows() data.datarow dim _categorylist new maincatlist _categorylist.categorylist = new list(of maincategory) if ds.rows.count > 0 = 0 ds.rows.count - 1 dim _category new maincategory dim id string = ds.rows(i)("ana katagori kodu").tostring.trim _category.anakatagorikodu = id _category.anakatagoriadi = ds.rows(i)("ana katagori adı").tostring.trim _category.anaurunaciklamasi = ds.rows(i)("ana Ürün açıklaması").tostring.trim _category.anaurunadi = ds.rows(i)("ana Ürün adı").tostring.trim _category.marka = ds.rows(i)("marka").tostring.trim _category.miktar = ds.rows(i)("miktar").tostring.trim _category.anaurunkodu = ds.rows(i)("ana Ürün kodu").tostring.trim _category.deepcategories = new list(of deepcategori) foundrows = subds.tables(0).select("[ana katagori kodu] = '" & id & "'") if foundrows.length - 1 > 0 x = 0 foundrows.length - 1 dim _subcategory new deepcategori _subcategory.kategorikodu = foundrows(x).item(0).tostring.trim _subcategory.kategori = foundrows(x).item(3).tostring.trim ' subds.rows(x)("katagori").tostring.trim. _subcategory.renk = foundrows(x).item(8).tostring.trim 'subds.rows(x)("renk").tostring.trim. _subcategory.beden = foundrows(x).item(9).tostring.trim ' subds.rows(x)("beden").tostring.trim. _subcategory.barkod = foundrows(x).item(10).tostring.trim ' subds.rows(x)("barkod").tostring.trim. _subcategory.miktar = foundrows(x).item(11).tostring.trim ' subds.rows(x)("miktar").tostring.trim. _subcategory.urunkodu = foundrows(x).item(12).tostring.trim ' subds.rows(x)("Ürün kodu").tostring.trim. _subcategory.urunadi = foundrows(x).item(13).tostring.trim ' subds.rows(x)("Ürün adı").tostring.trim. _subcategory.fiyat = foundrows(x).item(14).tostring.trim ' subds.rows(x)("fiyat").tostring.trim. _category.deepcategories.add(_subcategory) next x end if _categorylist.categorylist.add(_category) next end if da.dispose() ds.dispose() subds.dispose() 'dim empty string = string.empty empty = jsonconvert.serializeobject(_categorylist, newtonsoft.json.formatting.indented) 'context.response.write(empty) if u use direct post web page end using catch ex exception ' context.response.write(ex.message) end try `
Comments
Post a Comment