zoukankan      html  css  js  c++  java
  • Asp.Net Newtonsoft.Json使用教程

    json序列化和反序列化的使用教程

    实体

    复制代码

    public class wendaModel
    {
        private string _title;
        private string _cons;
    
        public string title
        {
            set { _title = value; }
            get { return _title; }
        }
        public string cons
        {
            set { _cons = value; }
            get { return _cons; }
        }
    }

    复制代码

    1、对象转换为json字符串(序列化)

    复制代码

    wendaModel model = new wendaModel();
    model.title = "json";
    model.cons = "asp.net";
    
    string json = Newtonsoft.Json.JsonConvert.SerializeObject(model);
    Response.Write(json);
    //输入结果:{"title":"json","cons":"asp.net"}

    复制代码

    2、List转换为json字符串(序列化)

    复制代码

    List<wendaModel> list = new List<wendaModel>();
    for (int i = 0; i < 3; i++)
    {
        wendaModel model = new wendaModel();
        model.title = "json" + i.ToString();
        model.cons = "asp.net" + i.ToString();
        list.Add(model);
    }
    string json = Newtonsoft.Json.JsonConvert.SerializeObject(list);
    Response.Write(json);
    //输出结果:[{"title":"json0","cons":"asp.net0"},{"title":"json1","cons":"asp.net1"},{"title":"json2","cons":"asp.net2"}]

    复制代码

    3、json字符串转换为对象(反序列化)

    string json = "{"cons":"asp.net","title":"json"}";
    wendaModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<wendaModel>(json);
    Response.Write(model.title + "=" + model.cons);
    //输出结果:json=asp.net

    4、json字符串转换为对象(反序列化)

    复制代码

    string json = "[{"cons":"asp.net2","title":"json2"},{"cons":"asp.net1","title":"json1"}]";
    List<wendaModel> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<wendaModel>>(json);
    foreach (var item in list)
    {
         Response.Write(item.title);
    }

    原文地址:https://www.cnblogs.com/webapi/p/10621376.html

  • 相关阅读:
    Linux mysql 远程访问
    Linux下高并发socket最大连接数所受的各种限制
    Linux之gunzip命令
    不停在终端中报log
    FIO测试
    yum是什么?(linux命令)
    ubuntu grub 登录
    百度网盘命令行方式,解决ubuntu16.04百度网盘无法运行的问题
    excel使用经验汇总
    ubuntu 安装 ipfs 经验
  • 原文地址:https://www.cnblogs.com/songjuntao/p/15260394.html
Copyright © 2011-2022 走看看