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

代碼如下:

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

以上就是這篇文章的全部內容瞭,希望本文的內容對大傢的學習或者工作具有一定的參考學習價值,謝謝大傢對WalkonNet的支持。如果你想瞭解更多相關內容請查看下面相關鏈接

推薦閱讀: