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"
}

 

var jObj = JObject.Parse(paramsStr);    //paramsStr - json字符串
var shp_flg = jObj.Value<string>("shp_flg");
var fm_date = jObj.Value<DateTime>("fm_date").ToString("yyyy-MM-dd");
var to_date = jObj.Value<DateTime>("to_date").ToString("yyyy-MM-dd");
var emp_no = jObj.Value<string>("emp_no");
var shift = jObj.Value<string>("shift");
var work_time = jObj.Value<int>("work_time");
var remark = jObj.Value<string>("remark");

 

二、嵌套json对象

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

/*   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"

 

我自己测试解析复杂的json也可以 =====start

 Http.Get("http://xxxxx.com/apis/index.php?action=main").OnSuccess(result =>
 {

    Newtonsoft.Json.Linq.JObject resInfo = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result);
    //Console.WriteLine(resInfo);
    Console.WriteLine(resInfo["data"]["goods_list"][0]["goods_name"]);
        
}).OnFail(webexception =>
{
    Console.WriteLine(webexception.Message);
}).Go();

 

C# 通过JObject解析复杂 json字符串

=====end

 

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

https://www.jb51.net/article/138291.htm

http://www.voidcn.com/article/p-zmjnotmh-bxz.html

 

dynamic 方式接受解析json (还在研究中....)

https://www.cnblogs.com/buguge/p/6126363.html

https://www.cnblogs.com/linJie1930906722/p/5789413.html

 

C# 通过JObject解析复杂 json字符串

上一篇:Win32 滚动条控件


下一篇:zabbix_api 接口调用