zoukankan      html  css  js  c++  java
  • 初探Newtonsoft.json

    Json的序列化和反序列化是个使用面很广的东西;

    原因就在于当你开始使用webapi进行数据交互的时候,很多的操作就需要依靠反序列化将传递的json数据解析即可;

    下载方式可以用管理NuGet包进行下载,注意这里需要配置NuGet的包地址:

     注意源地址为:https://www.nuget.org/api/v2/

    示例代码如下:

    客户端:

     1 string apiUrl = "http://localhost:3120/";
     2 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(apiUrl+ @"api/Student");
     3 req.Method = "GET";
     4 //req.ContentType = "application/json";
     5 
     6 HttpWebResponse res = (HttpWebResponse)req.GetResponse();
     7 Stream resStream = res.GetResponseStream();
     8 StreamReader strReader = new StreamReader(resStream, Encoding.UTF8);
     9 string data = strReader.ReadToEnd();
    10 List<Student> listData = JsonConvert.DeserializeObject<List<Student>>(data);//反序列化

    服务端API:

     1   public class StudentController : ApiController
     2     {
     3         public List<Student> GetStudentsInfo()
     4         {
     5             Student s1 = new Student() { ID = 1, Name = "Lee", Age = 12 };
     6             Student s2 = new Student() { ID = 2, Name = "Tom", Age = 22 };
     7             Student s3 = new Student() { ID = 3, Name = "Han", Age = 34 };
     8             Student s4 = new Student() { ID = 4, Name = "Keven", Age = 14 };
     9             Student s5 = new Student() { ID = 5, Name = "Loce", Age = 43 };
    10             Student s6 = new Student() { ID = 6, Name = "KAka", Age = 54 };
    11             List<Student> res = new List<Student>() { s1, s2, s3, s4, s5,s6 };
    12             return res;
    13         }      
    14     }
  • 相关阅读:
    [硬件]_ELVE_VS2015下opencv3.3的配置问题
    [Linux]_ELVE_ssh登录远程阿里服务器
    [python]_ELVE_pip2和pip3如何共存
    U盘无法打开提示格式化?如何进行恢复
    [转]pycharm的一些快捷键
    文件上传Django
    ansible编译安装--操作系统环境Redhat6.4
    django的models字段介绍
    paramiko模块
    mysql安装等操作
  • 原文地址:https://www.cnblogs.com/LeeSki/p/12153189.html
Copyright © 2011-2022 走看看