简体   繁体   中英

json serialization and deserialization in c#

I have created a rough json data sample.Please corporate and try to help me for the required solution.

I want to now the class structures and how to retrieve and also make json of this format through c#.I have four tables-tblcategory(contains all categories),tblcatmapping(contains the category mapping),tblitemmapping(contains the mapping of item and category) and tblitem(contains all the items)

 [
{
    "CATID": "10001",
    "CATNAME": "food",
    "CATTYPE": "top",
    "CATDESC": "contains different types of food",
    "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 11-41-29-food.jpg",
    "CATSUBCAT": [
        {
            "CATID": "10002",
            "CATNAME": "veg",
            "CATTYPE": "sub",
            "CATDESC": "contains different drinks",
            "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
            "CATSUBCAT": [
                {
                    "CATID": "10004",
                    "CATNAME": "nort-indian",
                    "CATTYPE": "sub",
                    "CATDESC": "contains different drinks",
                    "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
                    "CATSUBCAT": [],
                    "CATITEM": [
                        {
                            "ITEMID": "20002",
                            "ITEMNAME": "dosa",
                            "ITEMTYPE": "item",
                            "ITEMDESC": "contains different dosa",
                            "ITEMIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg"
                        },
                        {
                            "ITEMID": "20003",
                            "ITEMNAME": "idli",
                            "ITEMTYPE": "item",
                            "ITEMDESC": "contains different idli",
                            "ITEMIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg"
                        }
                    ]
                },
                {
                    "CATID": "10005",
                    "CATNAME": "south-indian",
                    "CATTYPE": "sub",
                    "CATDESC": "contains different drinks",
                    "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
                    "CATSUBCAT": [],
                    "CATITEM": []
                }
            ],
            "CATITEM": []
        },
        {
            "CATID": "10003",
            "CATNAME": "non-veg",
            "CATTYPE": "sub",
            "CATDESC": "contains different drinks",
            "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
            "CATSUBCAT": [
                {
                    "CATID": "10004",
                    "CATNAME": "nort-indian",
                    "CATTYPE": "sub",
                    "CATDESC": "contains different drinks",
                    "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
                    "CATSUBCAT": [],
                    "CATITEM": []
                },
                {
                    "CATID": "10005",
                    "CATNAME": "south-indian",
                    "CATTYPE": "sub",
                    "CATDESC": "contains different drinks",
                    "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
                    "CATSUBCAT": [],
                    "CATITEM": []
                }
            ],
            "CATITEM": []
        }
    ],
    "CATITEM": []
},
{
    "CATID": "10006",
    "CATNAME": "beverages",
    "CATTYPE": "top",
    "CATDESC": "contains different drinks",
    "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
    "CATSUBCAT": [
        {
            "CATID": "10007",
            "CATNAME": "alcholic-drinks",
            "CATTYPE": "top",
            "CATDESC": "contains different drinks",
            "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
            "CATSUBCAT": [],
            "CATITEM": []
        },
        {
            "CATID": "10008",
            "CATNAME": "non-alcholic-drinks",
            "CATTYPE": "top",
            "CATDESC": "contains different drinks",
            "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg",
            "CATSUBCAT": [],
            "CATITEM": []
        }
    ],
    "CATTITEM": []
},
{
    "CATID": "10009",
    "CATNAME": "fast food",
    "CATTYPE": "top",
    "CATDESC": "contains various fast foods",
    "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-22-08-fastfood1.jpg",
    "CATSUBCAT": [],
    "CATITEM": [
        {
            "ITEMID": "20002",
            "ITEMNAME": "dosa",
            "ITEMTYPE": "item",
            "ITEMDESC": "contains different dosa",
            "ITEMIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg"
        },
        {
            "ITEMID": "20003",
            "ITEMNAME": "idli",
            "ITEMTYPE": "item",
            "ITEMDESC": "contains different idli",
            "ITEMIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\09-09-2013 12-15-19-beverages.jpeg"
        }
    ]
},
{
    "CATID": "10015",
    "CATNAME": "test",
    "CATTYPE": "top",
    "CATDESC": "contains test categories",
    "CATIMGPATH": "Z:\\TFS workspace\\DineGenie - Dev\\DineGenie - Dev\\Filestore\\category\\19-09-2013 15-46-19-southindian2.png",
    "CATSUBCAT": [],
    "CATITEM": []
}
]

From the below suggestions i tried to convert the json sample to c# code by using http://json2csharp.com/ >Following is the :

class structure

public class RootObject
{
public string CATID { get; set; }
public string CATNAME { get; set; }
public string CATTYPE { get; set; }
public string CATDESC { get; set; }
public string CATIMGPATH { get; set; }
public List<object> CATSUBCAT { get; set; }
public List<object> CATITEM { get; set; }
public List<object> CATTITEM { get; set; }

}

But i am not getting in the above object is of what type

You can use this site http://json2csharp.com/ to create the class structure for you. I tried and like you said there are errors in your json.

Edit nr Helping fix Json: Check your CATSUBCAT for errors your not open and closing it right

{
"CATID": "10001",
"CATNAME": "food",
"CATTYPE": "top",
"CATDESC": "contains different types of food",
"CATSUBCAT": [
              {
                "CATID": "10002",
                "CATNAME": "veg",
                "CATTYPE": "sub",
                "CATDESC": "contains different drinks"
 },
 {
                "CATID": "10002",
                "CATNAME": "veg",
                "CATTYPE": "sub",
                "CATDESC": "contains different drinks"
 }

]
}

Edit nr 2: Newtonsoft.Json

Lets say you have all that Json loaded into a nice string.

string data = "{..... Json crap......}";

then all you have to do is use it to parse the json for you.

Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(data);

Once its all parsed into jObject you can then read from it.

String CATID= jObject.SelectToken("CATID").ToSTring(); // will give you 10001

Now your subcats are arrays so thats a Jarray

JArray dataCats = (JArray)jObject.SelectToken("CATSUBCATS"); 

These then you can loop though

  foreach (JArray dataRow in dataRows)
        {
        String CATID= jObject.SelectToken("CATID").ToSTring(); // will give you 10001

        }

Hope it helps.

You can use any tools you'd like depending on your requirements some tools will fit better in your project. Even if you prefer to go with the .NET Framework built-in tools such as the DataContractJsonSerializer . But fif you want to make work you will need to clean the structure of the json object.

The main issue I can see in your json object is the nested arrays. Instead of doing things like...

{
    ...

    "CATSUBCAT":
        {
           [
              {
                    "CATID": "10002",
                    "CATNAME": "veg",

                    ...

                }
            ]
        }
}

do this...

{ ...

    "CATSUBCAT":
           [
              {
                    "CATID": "10002",
                    "CATNAME": "veg",

                    ...

                }
            ]
}

Note that arrays in json are declared as [], so if SUBCAT is an array of objects then the objects inside this array must be inside [] and separated by commas. Follow this link and check how employees are represented

Depending on the Visual Studio version you're working with, you can try Edit -> Paste Special -> Paste JSON As Classes .

See here for an example.

For serialization and deserialization I can highly recommend JSON.NET . It has way more features than the default .NET serializers.

If you have the classes and use JSON.NET, you can deserialize your string like this:

MyClass[] instances = JsonConvert.DeserializeObject<MyClass[]>(jsonString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM