Currently I'm writing a mobile app with Xamarin.Forms and my problem is, that I need the response from my API in separate variables instead of one string output.
My API output:
{"error":false,"user":{"id":3,"email":"root@root.de","vorname":"root","nachname":"toor","wka":"wka1"}}
I'm using Newtonsoft to deserialize the response and I think that the problem is the curly bracket behind "user":{...}
because I can print out public bool error { get; set; }
public bool error { get; set; }
public bool error { get; set; }
but the other vars are not working.
class JsonContent
{
public bool error { get; set; }
public int id { get; set; }
public string email { get; set; }
public string vorname { get; set; }
public string nachname { get; set; }
public string wka { get; set; }
}
Tests:
JsonContent j = JsonConvert.DeserializeObject<JsonContent>(response.Content);
bool pout = j.error; //output: false
JsonContent j = JsonConvert.DeserializeObject<JsonContent>(response.Content);
int pout = j.id; //output: 0
The C# class that you have for your JSON is not correct.
It should be
public class User
{
public int id { get; set; }
public string email { get; set; }
public string vorname { get; set; }
public string nachname { get; set; }
public string wka { get; set; }
}
public class JsonContent
{
public bool error { get; set; }
public User user { get; set; }
}
and then you can deserialize your JSON to your C# objects
You can use some json to c# converter to get the model, ie https://jsonutils.com , http://json2csharp.com . It will help you when you have to get the model of a big json.
public class User
{
public int id { get; set; }
public string email { get; set; }
public string vorname { get; set; }
public string nachname { get; set; }
public string wka { get; set; }
}
public class Example
{
public bool error { get; set; }
public User user { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.