Newtonsoft.Json 通过 JObject 读取 json对像 超简单

时间:2022-02-07 11:02:15
                        /*   json 格式的字符串解析 格式化
{
"input": {
"size": 193156,
"type": "image/png"
},
"output": {
"size": 59646,
"type": "image/png",
"width": 487,
"height": 284,
"ratio": 0.3088,
"url": "https://api.tinify.com/output/hrqtghqtv0ab4qgv.png"
}
}
*/ // json解析 嵌套格式
Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText); decimal input_size = Convert.ToDecimal(jobject["input"]["size"]);//193156, 输入图片大小
string input_type = jobject["input"]["type"].ToString();// "image/png",输入图片类型 decimal output_size = Convert.ToDecimal(jobject["output"]["size"]);// 59646, 图片大小
string output_type = jobject["output"]["type"].ToString();//"image/png", 图片类型
string output_width = jobject["output"]["width"].ToString();//487, 宽度
string output_height = jobject["output"]["height"].ToString();//284, 高度
string output_ratio = jobject["output"]["ratio"].ToString();//0.3088, 压缩率=Convert.ToString((1-0.3088)*100)+"%";
string output_url = jobject["output"]["url"].ToString();//"https://api.tinify.com/output/hrqtghqtv0ab4qgv.png" WebEnh.Core.Net.HttpProc.WebClient wc = new WebEnh.Core.Net.HttpProc.WebClient();
wc.DownloadFile(output_url, download_filename);