简体   繁体   English

C#中的json序列化和反序列化

[英]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. 我已经创建了一个粗略的json数据示例。请与公司联系,并尝试为我提供所需的解决方案。

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) 我现在想了解类结构以及如何通过c#进行这种格式的json检索。我有四个表-tblcategory(包含所有类别),tblcatmapping(包含类别映射),tblitemmapping(包含项和类别的映射) )和tblitem(包含所有项目)

 [
{
    "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 : 从以下建议中,我尝试通过使用http://json2csharp.com/将json示例转换为c#代码。

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. 您可以使用此站点http://json2csharp.com/为您创建类结构。 I tried and like you said there are errors in your json. 我尝试过,就像您说的那样,您的json中有错误。

Edit nr Helping fix Json: Check your CATSUBCAT for errors your not open and closing it right 编辑nr帮助修复Json:检查您的CATSUBCAT是否有错误,您无法正确打开和关闭它

{
"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 编辑nr 2:Newtonsoft.Json

Lets say you have all that Json loaded into a nice string. 可以说,您已经将Json的所有内容加载到了一个不错的字符串中。

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

then all you have to do is use it to parse the json for you. 那么您所要做的就是使用它为您解析json。

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

Once its all parsed into jObject you can then read from it. 一旦将其全部解析为jObject,您就可以从中读取内容。

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

Now your subcats are arrays so thats a Jarray 现在您的子猫是数组,所以这是一个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 . 即使您更喜欢使用.NET Framework内置工具,例如DataContractJsonSerializer But fif you want to make work you will need to clean the structure of the json object. 但是,如果要进行工作,则需要清理json对象的结构。

The main issue I can see in your json object is the nested arrays. 我可以在您的json对象中看到的主要问题是嵌套数组。 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. 请注意,json中的数组声明为[],因此,如果SUBCAT是对象数组,则此数组内的对象必须位于[]内并以逗号分隔。 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 . 根据您使用的Visual Studio版本,您可以尝试Edit -> Paste Special -> Paste JSON As Classes

See here for an example. 请参阅此处的示例。

For serialization and deserialization I can highly recommend JSON.NET . 对于序列化和反序列化,我强烈建议JSON.NET It has way more features than the default .NET serializers. 它具有比默认.NET序列化器更多的功能。

If you have the classes and use JSON.NET, you can deserialize your string like this: 如果您拥有这些类并使用JSON.NET,则可以像这样反序列化您的字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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