将Json指向变量的问题

时间:2015-07-24 10:54:47

标签: c# json

我有以下Json回复:

{"error":"false","_id":1,"_avatar":"","_name":"Somename","_surname":"Somesurname","_email":"something@domain.com","_gsm":"301313","_password":"de44df5016dfeac28ff7587b57d34dbfa205e188","_language":1,"_phone":"","_company":"","_vat":"","_registrationDate":"2015-07-07 09:55:31","_address":8,"_lastLogin":"2015-07-24 09:06:45","_username":"something@domain.com","_salt":"ewqJWRNnGyVJv+q6NCwR3v4h+ak1ecWt","_hash":"","_activated":true,"_timeZone":0}

问题是网站中的vairbales是私有的,因此名称以" _"开头。如何在C#中解析这些变量,而没有公共变量的下划线?

现在班级如何:

public class UserModel {
    public string error { get; set; }

    public string errorType { get; set; }

    public string errorMessage { get; set; }

    public string sessionId { get; set; }

    public string id { get; set; }

    public string name { get; set; }

    public string surname { get; set; }

    public string email { get; set; }

    public string gsm { get; set; }

    public string company { get; set; }

    public string address { get; set; }

    public string avatar { get; set; }
}

这是我反序列化的方式:

string urlLogIn = "http://10.1.1.20/appapi/login";
JsonValue jsonLogIn = await jsonFetcher.FetchJsonAsync(urlLogIn, email.Text, password.Text, null);
userModel = JsonConvert.DeserializeObject<UserModel>(jsonLogIn);

/...

public class JsonFetcher {
        private const string API_KEY = "***********************************";

        public async Task<JsonValue> FetchJsonAsync(string url, string email, string pass, string cookie) {
            var client = new WebClient();
            Uri uri = new Uri(url);
            NameValueCollection values = new NameValueCollection();

            if (email == null && pass == null) {
                values.Add("key", API_KEY);
                client.Headers.Add(HttpRequestHeader.Cookie, "PHPSESSID=" + cookie);
            } else if (pass == null) {
                values.Add("email", email);
                values.Add("key", API_KEY);
            } else {
                values.Add("email", email);
                values.Add("pass", pass);
                values.Add("key", API_KEY);
            }

            byte[] encodedResponse = await Task.Run(() => client.UploadValues(uri, values));
            JsonValue jsonResponse = Encoding.Default.GetString(encodedResponse);
            Console.WriteLine("Response:" + jsonResponse);
            return jsonResponse;
        }
    }

0 个答案:

没有答案