C#关于json的一些操作
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#中Newtonsoft.Json(Json.NET)的使用
这个链接有一些关于C#处理json格式数据的内容