C#中Newtonsoft.Json(Json.NET)的使用和处理json格式数据
C#关于json的一些操作
- C#中Newtonsoft.Json(Json.NET)的使用
- 添加Newtonsoft.Json.dll引用:
- 下载Newtonsoft.Json.dll:
- 在项目中添加:
- Newtonsoft.Json的使用
- 添加头文件引用
- json格式的使用代码
- json格式的处理
- 如何将http的request的json格式数据写入到我们的类中
- 比较好的链接
- C#如何生成JSON字符串提交给接口(服务器):
- C#中Newtonsoft.Json(Json.NET)的使用
C#中Newtonsoft.Json(Json.NET)的使用
添加Newtonsoft.Json.dll引用:
处理json格式需要添加Newtonsoft.Json.dll
下载Newtonsoft.Json.dll:
Newtonsoft.Json的地址:
官网:http://json.codeplex.com/
源码地址:https://github.com/JamesNK/Newtonsoft.Json
Newtonsoft.Json.dll下载:https://github.com/JamesNK/Newtonsoft.Json/releases
在项目中添加:
Newtonsoft.Json的使用
添加头文件引用
using Newtonsoft.Json;
json格式的使用代码
调用代码:
//获取图书列表
List<BookInfo> bookList = GetBookList();//将图书列表转换成Json
string bookListJson = JsonConvert.SerializeObject(bookList);//将Json转换回图书列表
List<BookInfo> books = JsonConvert.DeserializeObject<List<BookInfo>>(bookListJson);
实体类代码:
/// <summary>
/// 图书信息实体类
/// </summary>
public class BookInfo
{public int BookId { set; get; } //图书ID public string Title { set; get; } //图书名称 public string Category { set; get; } //图书分类 public string Author { set; get; } //图书作者 public DateTime PublishDate { set; get; } //出版时间 public Double Price { set; get; } //销售价格
}
全部代码:
/// <summary>
/// 获取图书列表
/// </summary>
/// <returns></returns>
public List<BookInfo> GetBookList()
{List<BookInfo> bookList = new List<BookInfo>();BookInfo book1 = new BookInfo(){BookId = 1,Category = "CHILDREN",Title = "Harry Potter",Author = "J K. Rowling",PublishDate = new DateTime(2005, 08, 15),Price = 29.99};bookList.Add(book1);BookInfo book2 = new BookInfo(){BookId = 2,Category = "WEB",Title = "Learning XML",Author = "Erik T. Ray",PublishDate = new DateTime(2003, 10, 18),Price = 39.95};bookList.Add(book2);return bookList;
}
补充:如果某个字段不想被Json序列化,则可以在该字段上加上[Newtonsoft.Json.JsonIgnore]特性。
例如上述实例中的价格不想被Json序列化:
[Newtonsoft.Json.JsonIgnore]
public Double Price { set; get; } //销售价格
json格式的处理
第一步就是要根据这个JSON来写出对应的实体类。用来存放数据。这个实体类如何写的?其实非常简单。因为一般不需要手动自己写,当然,你要是喜欢也可以自己写。不过我一般使用网站直接转换。自己百度 查一下,JSON转C#实体类,就会有很多网站给你转。
网站:http://www.bejson.com/convert/json2csharp/
这个网站可以根据输入的json格式输出对应的实体类。
例如:
{"carriage":{"class_name":"carriage","height":145,"position":[55,254,815,399],"width":760},"code":0,"costTime":0.23,"truck":{"class_name":"truck","height":222,"position":[73,237,1044,459],"width":971},"truckfront":{"class_name":"truckfront","height":200,"position":[834,237,1044,437],"width":210}}
生成对应的实体类:
public class Carriage
{/// <summary>/// /// </summary>public string class_name { get; set; }/// <summary>/// /// </summary>public int height { get; set; }/// <summary>/// /// </summary>public List <int > position { get; set; }/// <summary>/// /// </summary>public int width { get; set; }
}public class Truck
{/// <summary>/// /// </summary>public string class_name { get; set; }/// <summary>/// /// </summary>public int height { get; set; }/// <summary>/// /// </summary>public List <int > position { get; set; }/// <summary>/// /// </summary>public int width { get; set; }
}public class Truckfront
{/// <summary>/// /// </summary>public string class_name { get; set; }/// <summary>/// /// </summary>public int height { get; set; }/// <summary>/// /// </summary>public List <int > position { get; set; }/// <summary>/// /// </summary>public int width { get; set; }
}public class Root
{/// <summary>/// /// </summary>public Carriage carriage { get; set; }/// <summary>/// /// </summary>public int code { get; set; }/// <summary>/// /// </summary>public double costTime { get; set; }/// <summary>/// /// </summary>public Truck truck { get; set; }/// <summary>/// /// </summary>public Truckfront truckfront { get; set; }
}
正确的JSON!
如何将http的request的json格式数据写入到我们的类中
private void button4_Click(object sender, EventArgs e){HttpPostFileRequestClient test1 = new HttpPostFileRequestClient();string fileUrl = "truck_2.jpg";FileStream fs = new FileStream("F:\\all_truck\\truck_3.jpg", FileMode.Open, FileAccess.Read);test1.SetField("file", "truck_3.jpg", "multipart/form-data", fs);HttpWebResponse response = test1.Post("http://127.0.0.1:5000/measure/");Stream myResponseStream = response.GetResponseStream();StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);string retString = myStreamReader.ReadToEnd();MessageBox.Show(retString);System.Diagnostics.Debug.WriteLine(retString);//这个需要引入Newtonsoft.Json这个DLL并using//传入我们的实体类还有需要解析的JSON字符串这样就OK了。然后就可以通过实体类使用数据了。Root rt = JsonConvert.DeserializeObject<Root>(retString);//这样就可以取出json数据里面的值int truck_hgight = rt.truck.height;MessageBox.Show("code=" + rt.code + "\r\n" + "costTime=" + rt.costTime+"GIGHT"+ truck_hgight);//由于这个JSON字符串的 public List<DataItem> data 是一个集合,所以我们需要遍历集合里面的所有数据}
比较好的链接
C#如何生成JSON字符串提交给接口(服务器):
这个系列有4篇博文,详细介绍了http如何处理json格式数据,非常有用
第一章:C#如何拿到从http上返回JSON数据?
这个链接中包括c#的http的post和get的代码,主要介绍了如何获得json格式数据
第二章:C#如何解析JSON数据?(反序列化对象)
介绍了如何处理的json格式数据
第三章:C#如何生成JSON字符串?(序列化对象)
第四章:C#如何生成JSON字符串提交给接口(服务器)?
C#中Newtonsoft.Json(Json.NET)的使用
C#中Newtonsoft.Json(Json.NET)的使用和处理json格式数据相关推荐
- mysql中使用日期加减时无法识别年-月格式数据的问题,%Y-%m这种格式数据
最新做报表统计的时候处理按月统计部分时发现,虽然使用 DATE_FORMAT( time, '%Y-%m' ) 函数可以将日期格式转成年-月,但是如果是参数是年-月格式,即"2018-10& ...
- java poi 读取Excel中的手机号或电话号码,手机号变成1.8312345678E10格式数据的解决方法
[现象] 共有2行数据 [张三1, 1.8312345678E10, 张三2, 1.8312345679E10] [解决方法] v.add(row.getCell(1)+""); ...
- C#中Newtonsoft.Json(Json.NET)的使用
C#中Newtonsoft.Json(Json.NET)的使用. 添加引用: using Newtonsoft.Json; 调用代码: //获取图书列表 List<BookInfo> bo ...
- Json.net/Newtonsoft 3.0 新特性JObject/Linq to Json
Json.net codeplex :http://www.codeplex.com/Json 原本感觉Newtonsoft.Json和.net自己的JavaScriptSerializer相差无几, ...
- 数据库中省区市组装成json_全国各省、市、县、镇、村的mysql数据库和JSON格式数据...
------------------ 直接开门见山了,放出下载地址先: phpmyadmin压缩导出的mysql库,库名:ajing,内有6个表,一个表不带后缀的是原始数据,每一行是一个村,从省至村: ...
- python中json模块_Python使用内置json模块解析json格式数据的方法
本文实例讲述了Python使用内置json模块解析json格式数据的方法.分享给大家供大家参考,具体如下: Python中解析json字符串非常简单,直接用内置的json模块就可以,不需要安装额外的模 ...
- python中json模块博客园_python的json模块
python的json模块 什么是序列化和反序列化? 序列化 在分布式环境下,无论是何种数据,都会以二进制序列的形式在网络上传输.序列化是一种将对象以一连串的字节描述的过程,用于解决在对对象流进行读写 ...
- 在JSP页面中输出JSON格式数据
JSON-taglib是一套使在JSP页面中输出JSON格式数据的标签库. JSON-taglib主页: http://json-taglib.sourceforge.net/index.html J ...
- python读json文件中不同的数据类型_怎么使用python提取json文件中的字段
python中为什么用json有什么作用 python的json模块中如何将变量添加到里面 python的json模块第一个是要打开的文件,第二个是打开的操作,为什么会如果你早认清你在别人心中没那么重 ...
最新文章
- 云效 Flow——Java构建并通过云效上传二方库到 Maven 私有仓库
- ITK:平移一个网格
- 职高学的计算机单招考试能换专业吗,2020年湖南中专生单招考大专能换专业吗?...
- Kaggle API
- unicode编码查询方法
- openMVG跑自定义数据出错
- jsfor循环终止_js 终止 forEach 循环
- LeetCode 题 - 58. 最后一个单词的长度 python解答
- Selenium+Python自动化测试学习问题总结笔记
- windows各版本序列号集合
- 数据错误(循环冗余检查) 各种解决方法
- 私域运营第五讲:实体店私域流量拉新实体餐饮店如何通过搭建私域流量实现营收增长
- 前端参数无法转为后端实体内部类_javaweb实现自动将前端的属性值(HttpServletRequest)转化为后台对应的实体类...
- IDEA配置优化,idea配置大全
- 核心单词Word List 41
- JAVA五子棋AI(人机对战 颜色选择 悔棋等 可直接使用)
- 菜鸟哥玩蓝牙Ble4.0系列 ESP32初玩篇③ Beacon —— 广播应用,室内定位,内容推送
- OracleRMAN备份与恢复之五:CONTROLFILE异常修复[ORA-00210、ORA-00202]
- axios-妙味课堂
- C# 根据汉字获取首字母拼音或全拼