0

I have the following Json response:

{"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}

The problem is the vairbales in the site are private so the names start with a "_". How can I parse these variables in C#, without an underscore to public variables ?

How the class looks like now:

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

This is how I deserialize it:

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;
        }
    }
Denki
  • 363
  • 2
  • 14
  • Are you already using any particular JSON parser? – James Thorpe Jul 24 '15 at 10:55
  • 1
    Depends on your parser. See [Json.Net Rename properties during serialization](http://stackoverflow.com/questions/19873767/json-net-rename-properties-during-serialization) for JSON.NET, [Changing property names for serializing](http://stackoverflow.com/questions/12456075/changing-property-names-for-serializing) for DataContractJsonSerializer. If you have other code, show your code. And your "Nope" isn't really helpful, show how you currently deserialize this JSON. – CodeCaster Jul 24 '15 at 10:58
  • @CodeCaster Edited the question. – Denki Jul 24 '15 at 11:03
  • 1
    Yeah `JsonConvert` is JSON.NET, see duplicate to annotate your model with `[JsonProperty(Name="...")]` attributes. Actually [.Net NewtonSoft Json Deserialize map to a different property name](http://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name) is a better duplicate. – CodeCaster Jul 24 '15 at 11:08
  • @CodeCaster Thank you, it worked – Denki Jul 24 '15 at 13:46

0 Answers0