编程语言
首页 > 编程语言> > C# 通过JObject解析json对象

C# 通过JObject解析json对象

作者:互联网

一、简单json对象

{
"shp_flg": "0",
"fm_date": "2018-04-18T00:00:00Z",
"to_date": "2018-04-18T00:00:00Z",
"emp_no": "008",
"emp_nme_en": "Visitor 6",
"shift": "事假",
"work_time": 35,
"remark": "xyz"
}
JObject jo = JObject.Parse(paramsStr);    //paramsStr - json字符串名字
//下面是各种不同写法,但是结果一样
string shp_flg = jo.["shp_flg"].ToString();
string fm_date = jo.["fm_date"].ToString("yyyy-MM-dd");
string to_date = jo.Value<DateTime>("to_date").ToString("yyyy-MM-dd");
string emp_no = jo.["emp_no"].ToString();
string shift = jo.Value<string>("shift");
int work_time = jo.Value<int>("work_time");
string remark = jo.Value<string>("remark");

 

二、嵌套json对象

 1 json 格式的字符串解析 格式化
 2 {
 3    "input": {
 4        "size": 193156, 
 5        "type": "image/png"
 6    }, 
 7    "output": {
 8        "size": 59646, 
 9        "type": "image/png", 
10        "width": 487, 
11        "height": 284, 
12        "ratio": 0.3088, 
13        "url": "https://api.tinify.com/output/hrqtghqtv0ab4qgv.png"
14     }
15 }
16 
17 // json解析  嵌套格式
18 Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText);
19 
20 decimal input_size = Convert.ToDecimal(jobject["input"]["size"]);//193156, 输入图片大小
21 string input_type = jobject["input"]["type"].ToString();// "image/png",输入图片类型
22 decimal output_size = Convert.ToDecimal(jobject["output"]["size"]);// 59646, 图片大小
23 string output_type = jobject["output"]["type"].ToString();//"image/png", 图片类型
24 string output_width = jobject["output"]["width"].ToString();//487, 宽度
25 string output_height = jobject["output"]["height"].ToString();//284, 高度
26 string output_ratio = jobject["output"]["ratio"].ToString();//0.3088, 压缩率=Convert.ToString((1-0.3088)*100)+"%";
27 string output_url = jobject["output"]["url"].ToString();//"https://api.tinify.com/output/hrqtghqtv0ab4qgv.png"
28 
29 
30 WebEnh.Core.Net.HttpProc.WebClient wc = new WebEnh.Core.Net.HttpProc.WebClient();
31 wc.DownloadFile(output_url, download_filename);

 转载地址:https://blog.csdn.net/chelen_jak/article/details/79987799

标签:string,C#,JObject,json,jo,ToString,jobject,date,output
来源: https://www.cnblogs.com/Dumb-dog/p/11511883.html