c# jobject 的數據結構的解析


首先下載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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM