簡體   English   中英

將json反序列化為C#中的類

[英]Deserialize json into class in C#

json="{\n \"kind\": \"plus#person\",
    \n \"etag\": \"\\\"GjejyguyfTE/pdfBI8xyufoiuh9bOLZ8VyG_8\\\"\",
    \n \"gender\": \"male\",
    \n \"emails\": [\n  {\n   \"value\": \"example@gmail.com\",\n   \"type\": \"account\"\n  }\n ],
    \n \"objectType\": \"person\",
    \n \"id\": \"xxxxxxxxxxxxxxx\",
    \n \"displayName\": \"XXXXXXXXX XXXXXXX\",
    \n \"name\": {\n  \"familyName\": \"XXXXXX\",\n  \"givenName\": \"XXXXXX\"\n },
    \n \"url\": \"https://plus.google.com/xxxxxxxxxxxxxxx\",
    \n \"image\": {\n  \"url\": \"https://lh3.googleusercontent.com/-XdUWA/AAAAAAI/AAbbbbAA/42hhscbv5M/photo.jpg?sz=70\"\n },
    \n \"isPlusUser\": true,
    \n \"language\": \"en\",
    \n \"ageRange\": {\n  \"min\": 21\n },
    \n \"circledByCount\": 0,
    \n \"verified\": false\n}\n"



 public class GoogleUser
    {
        public string kind { get; set; }
        public string etag { get; set; }
        public string gender { get; set; }
        public Email[] emails { get; set; }
        public string objectType { get; set; }
        public string id { get; set; }
        public string displayName { get; set; }
        public Name name { get; set; }
        public string url { get; set; }
        public Image image { get; set; }
        public bool isPlusUser { get; set; }
        public string language { get; set; }
        public AgeRange ageRange { get; set; }
        public int circledByCount { get; set; }
        public bool verified { get; set; }
    }

    public class Name
    {
        public string familyName { get; set; }
        public string givenName { get; set; }
    }

    public class Email
    {
        public string value { get; set; }
        public string type { get; set; }
    }
    public class Image 
    {
        public string url { get; set; } 
    }
    public class AgeRange 
    {
        public int min { get; set; }
    }





GoogleUser  user = new JavaScriptSerializer().Deserialize<GoogleUser>(json);

我正在用C# 4.0開發,我在項目中login with google+按鈕login with google+ 我想通過使用access_token獲取用戶的。 我可以將它們作為json filen獲取。 然后,我想將它們復制到我的GoogleUser class 我沒有收到錯誤,但是user始終等於null 這樣,昨天我可以獲取用戶的數據,而今天卻不能。

我的問題是什么?

<code> string a </ code>不是<code> null </ code>,它是我的<code> json </ code>文件,但是<code> user </ code>是<code> null </ code >

你能試一下嗎:

user =  new JavaScriptSerializer().Deserialize<A>( a );

因為您說“ a”具有字符串中的值。 也許問題是您兩次調用ReadToEnd?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM