zoukankan      html  css  js  c++  java
  • JSON serializing and deserializing using JSON.NET

    实体类
    ///<summary>
    /// 用户信息
    ///</summary>
    publicclass userInfo
    {
    privatestring _name;
    publicstring name
    {
    get { return _name; }
    set { _name = value; }
    }

    privatestring _screen_name;
    publicstring screen_name
    {
    get { return _screen_name; }
    set { _screen_name = value; }
    }

    privatestring _location;
    publicstring location
    {
    get { return _location; }
    set { _location = value; }
    }
    }
    ///<summary>
    /// 消息
    ///</summary>
    publicclass timeLine
    {
    privatestring _created_at;
    publicstring created_at
    {
    get { return _created_at; }
    set { _created_at = value; }
    }

    privatestring _text;
    publicstring text
    {
    get { return _text; }
    set { _text = value; }
    }

    privateint _id;
    publicint id
    {
    get { return _id; }
    set { _id = value; }
    }

    privatestring _mms_img_pre;
    publicstring mms_img_pre
    {
    get { return _mms_img_pre; }
    set { _mms_img_pre = value; }
    }

    private userInfo _user;
    public userInfo user
    {
    get { return _user; }
    set { _user = value; }
    }
    }

    Serializing

    1、序列化一个对像

    代码
    timeLine obj =new timeLine();
    obj.created_at
    ="2010-7-15";
    obj.id
    =2589;
    obj.mms_img_pre
    ="http://www.abc.com";
    obj.text
    ="message text";
    obj.user
    = u;

    this.Literal1.Text = JsonConvert.SerializeObject(obj, Formatting.Indented);

    结果

    {
    "created_at": "2010-7-15",
    "text": "message text",
    "id": 2589,
    "mms_img_pre": "http://www.abc.com",
    "user": {
    "name": "jack",
    "screen_name": null,
    "location": "shenzhen"
    }
    }

    2、序列化一个对像的集合

    代码
    userInfo u =new userInfo();
    u.name
    ="jack";
    u.location
    ="shenzhen";

    timeLine obj
    =new timeLine();
    obj.created_at
    ="2010-7-15";
    obj.id
    =2589;
    obj.mms_img_pre
    ="http://www.abc.com";
    obj.text
    ="message text";
    obj.user
    = u;

    timeLine obj2
    =new timeLine();
    obj2.created_at
    ="2010-7-15";
    obj2.id
    =2589;
    obj2.mms_img_pre
    ="http://www.abc.com";
    obj2.text
    ="message text";

    List
    <timeLine> tls =new List<timeLine>();
    tls.Add(obj);
    tls.Add(obj2);

    this.Literal1.Text = JsonConvert.SerializeObject(tls, Formatting.Indented);

    结果

    代码
    [
    {
    "created_at": "2010-7-15",
    "text": "message text",
    "id": 2589,
    "mms_img_pre": "http://www.abc.com",
    "user": {
    "name": "jack",
    "screen_name": null,
    "location": "shenzhen"
    }
    },
    {
    "created_at": "2010-7-15",
    "text": "message text",
    "id": 2589,
    "mms_img_pre": "http://www.abc.com",
    "user": null
    }
    ]

    Deserializing

    原始JSON字符串

    代码
    [
    {
    "created_at": "Wed Jul 14 17:59:39 +0800 2010",
    "text": "[56fe] http://zuosa.com/Status/78385734",
    "id": 78385734,
    "mms_img_pre": "http://zuosa.com/photo/mmspv/00/11/69/2011136682.jpg",
    "mms_img": "http://zuosa.com/photo/mms/00/11/69/2011136682.jpg",
    "user": {
    "name": "Jack Cai",
    "screen_name": "jc2009",
    "location": "5e7f4e1c.6df15733"
    }
    },
    {
    "created_at": "Wed Jul 14 17:53:54 +0800 2010",
    "text": "[56fe] http://zuosa.com/Status/78384980",
    "id": 78384980,
    "mms_img_pre": "http://zuosa.com/photo/mmspv/00/11/69/3061136673.jpg",
    "mms_img": "http://zuosa.com/photo/mms/00/11/69/3061136673.jpg",
    "user": {
    "name": "Jack Cai",
    "screen_name": "jc2009",
    "location": "5e7f4e1c.6df15733"
    }
    },
    {
    "created_at": "Tue Jul 13 17:41:41 +0800 2010",
    "text": "5fc382e56ca167096816606f7684573065b9ff0c523054ea91cc90fd662f57286d416d6a3002",
    "id": 78228921,
    "user": {
    "name": "Jack Cai",
    "screen_name": "jc2009",
    "location": "5e7f4e1c.6df15733"
    }
    },
    {
    "created_at": "Wed Jul 07 10:50:07 +0800 2010",
    "text": "505a5565? 597d4f3c560054953002",
    "id": 77267585,
    "user": {
    "name": "Jack Cai",
    "screen_name": "jc2009",
    "location": "5e7f4e1c.6df15733"
    }
    }
    ]

    1、反序列化为一个对像

    timeLine msg = JsonConvert.DeserializeObject<timeLine>(json_input);

    2、反序列化为一个对像的集合

    List<timeLine> msg = JsonConvert.DeserializeObject<List<timeLine>>(json_input);
    关键字:JSON,JSON.NET,序列化,反序列化,解析
    http://chy710.cnblogs.com
  • 相关阅读:
    友链
    Vue打包后处理跨域
    es6 Promise
    express get和post数据
    Nodejs登陆注册应用
    bootstrap按钮
    vue-router
    vue--transition多个元素运动
    $ git push -u origin masterremote时出现错误: error: GH007: Your push would publish a private email address.
    vue.js监听
  • 原文地址:https://www.cnblogs.com/chy710/p/1778145.html
Copyright © 2011-2022 走看看