栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C#中Newtonsoft.Json(Json.NET)的使用和处理json格式数据

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 bookList = GetBookList();
 
//将图书列表转换成Json          
string bookListJson = JsonConvert.SerializeObject(bookList);
 
//将Json转换回图书列表
List books = JsonConvert.DeserializeObject>(bookListJson);

实体类代码:

///   
/// 图书信息实体类  
///   
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; }           //销售价格  
} 

全部代码:

///   
/// 获取图书列表  
///   
///   
public List GetBookList()
{
    List bookList = new List();
    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
{
    /// 
    /// 
    /// 
    public string class_name { get; set; }
    /// 
    /// 
    /// 
    public int height { get; set; }
    /// 
    /// 
    /// 
    public List  position { get; set; }
    /// 
    /// 
    /// 
    public int width { get; set; }
}
 
public class Truck
{
    /// 
    /// 
    /// 
    public string class_name { get; set; }
    /// 
    /// 
    /// 
    public int height { get; set; }
    /// 
    /// 
    /// 
    public List  position { get; set; }
    /// 
    /// 
    /// 
    public int width { get; set; }
}
 
public class Truckfront
{
    /// 
    /// 
    /// 
    public string class_name { get; set; }
    /// 
    /// 
    /// 
    public int height { get; set; }
    /// 
    /// 
    /// 
    public List  position { get; set; }
    /// 
    /// 
    /// 
    public int width { get; set; }
}
 
public class Root
{
    /// 
    /// 
    /// 
    public Carriage carriage { get; set; }
    /// 
    /// 
    /// 
    public int code { get; set; }
    /// 
    /// 
    /// 
    public double costTime { get; set; }
    /// 
    /// 
    /// 
    public Truck truck { get; set; }
    /// 
    /// 
    /// 
    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(retString);

            //这样就可以取出json数据里面的值
            int truck_hgight = rt.truck.height;


            MessageBox.Show("code=" + rt.code + "rn" + "costTime=" + rt.costTime+"GIGHT"+ truck_hgight);
            //由于这个JSON字符串的 public List 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#处理json格式数据的内容

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/903697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号