zoukankan      html  css  js  c++  java
  • VS中快速生成json数据格式对应的实体

    JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度。

    JSON就是一串字符串 只不过元素会使用特定的符号标注。

    {} 双括号表示对象

    [] 中括号表示数组

    "" 双引号内是属性或值

    : 冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)

    所以 {"name": "Michael"} 可以理解为是一个包含name为Michael的对象

    而[{"name": "Michael"},{"name": "Jerry"}]就表示包含两个对象的数组

    当然了,你也可以使用{"name":["Michael","Jerry"]}来简化上面一步,这是一个拥有一个name数组的对象。

    ps:现在还有很多人存在一些误区,为什么{name:'json'}在检验时通过不了,那是因为JSON官网最新规范规定:

    如果是字符串,那不管是键或值最好都用双引号引起来,所以上面的代码就是{"name":"json"}。

    下面是一个json字符串,先去验证JSON数据格式(http://www.bejson.com/)如下:

    {
        "items_custom_get_response": {
            "items": {
                "item": [
                    {
                        "num_iid": 1,
                        "product_id": 0,
                        "skus": [
                            {
                                "created": null,
                                "modified": null,
                                "outer_id": null,
                                "price": null,
                                "properties": null,
                                "properties_name": "黑色",
                                "quantity": "2",
                                "sku_id": null
                            }
                        ]
                    }
                ]
            }
        }
    }

     如果需要拿来用,肯定要反序列化,序列化成实体,结构如下:

    public class Rootobject
            {
                public Items_Custom_Get_Response items_custom_get_response { get; set; }
            }
     
            public class Items_Custom_Get_Response
            {
                public Items items { get; set; }
            }
     
            public class Items
            {
                public List<Item> item { get; set; }
            }
     
            public class Item
            {
                public int num_iid { get; set; }
                public int product_id { get; set; }
                public List<Sku> skus { get; set; }
            }
     
            public class Sku
            {
                public object created { get; set; }
                public object modified { get; set; }
                public object outer_id { get; set; }
                public object price { get; set; }
                public object properties { get; set; }
                public string properties_name { get; set; }
                public string quantity { get; set; }
                public object sku_id { get; set; }
            }
    使用VS2013或者2015可以快速生成对应的实体,就不需要自己动手写代码了。步骤如下:
    1、复制json字符串,然后选择编辑->选择性粘贴->将JSON粘贴为类。 vs截图然后就在类文件中生成下面文件:
    public class Rootobject
            {
                public Items_Custom_Get_Response items_custom_get_response { get; set; }
            }
     
            public class Items_Custom_Get_Response
            {
                public Items items { get; set; }
            }
     
            public class Items
            {
                public Item[] item { get; set; }
            }
     
            public class Item
            {
                public int num_iid { get; set; }
                public int product_id { get; set; }
                public Sku[] skus { get; set; }
            }
     
            public class Sku
            {
                public object created { get; set; }
                public object modified { get; set; }
                public object outer_id { get; set; }
                public object price { get; set; }
                public object properties { get; set; }
                public string properties_name { get; set; }
                public string quantity { get; set; }
                public object sku_id { get; set; }
            }

  • 相关阅读:
    一行代码解决各种IE兼容问
    Magento多语言设置——优化前台与后台实现方法
    建立多语言Magento网站
    让Dreamweaver支持phtml(支持更多的文件类型)
    netbox version 2.8build 4128 cannot initializes the scripting engine ...
    magento如何安装中文语言包
    如何查看 phtml文件 并有代码颜色提示
    国外博客网站推荐(75个)
    magento侧边栏如何增加模块
    如何查看 phtml文件 并有代码颜色提示
  • 原文地址:https://www.cnblogs.com/xuepei/p/5009359.html
Copyright © 2011-2022 走看看