隐藏

c# jobject 的数据结构的解析

发布:2020/4/2 16:14:23作者:管理员 来源:本站 浏览次数:1149

首先下载Newtonsoft.Json,增加引用using Newtonsoft.Json.Linq;
把jobject的内容提取出来,Jobject的内容格式如下:

 

{
 "code": 200,
 "msg": "SUCCESS",
 "data": {
  "id": "12345678",
  "name": "张三",
  "sex": "男",
  "result": {
   "access_token": "49d58eacd7811e463429a1ae10b42173",
   "user_info": [{
    "school": "社会大学",
    "major": "软件开发",
    "education": "本科",
    "score": 97
   }, {
    "school": "湖南大学",
    "major": "软件工程",
    "education": "研究生",
    "score": 100
   }]
  }
 }
}

可放到json官网在线JSON校验格式化工具里解析。

 

代码如下:                   

1,新建类:
        public class UserInfo
        {
            public string id { get; set; }
            public string name { get; set; }
            public string sex { get; set; }
            public string access_token { get; set; }
            public string school { get; set; }
            public string major { get; set; }
            public string education { get; set; }
            public string score { get; set; }
        }

2,获取值:

            JObject result = new JObject();//假设result为数据结构
            UserInfo userinfo = new UserInfo();
            userinfo.id = result["data"].Value<string>("id");//id
            userinfo.name = result["data"].Value<string>("name"); //name
            userinfo.sex = result["data"].Value<string>("sex"); //sex
            userinfo.access_token= result["data"]["result"]["access_token"].ToString();//access_token
            JArray res = result["data"]["result"].Value<JArray>("user_info");
            JObject obj = JObject.Parse(res[0].ToString());//只获取数据结构中第一个userinfo里的数据信息
            userinfo.school = obj.Value<string>("school"); //schoool
            userinfo.major = obj.Value<string>("major");//major
            userinfo.education = obj.Value<string>("education");//education
            userinfo.score= obj.Value<string>("score");//score

我爱代码,代码使我快乐!


{
"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 格式的字符串解析 格式化
{
   "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);