C# 对数组C的Json响应中的对象进行反序列化

C# 对数组C的Json响应中的对象进行反序列化,c#,xamarin,serialization,json.net,C#,Xamarin,Serialization,Json.net,目前我正在用Xamarin.Forms编写一个移动应用程序,我的问题是,我需要API在单独的变量中响应,而不是一个字符串输出 我的API输出: {"error":false,"user":{"id":3,"email":"root@root.de","vorname":"root","nachname":"toor","wka":"wka1"}} 我正在使用Newtonsoft对响应进行反序列化,我认为问题在于用户{…}后面的花括号,因为我可以打印出公共bool错误{get;set;},但其他

目前我正在用Xamarin.Forms编写一个移动应用程序,我的问题是,我需要API在单独的变量中响应,而不是一个字符串输出

我的API输出:

{"error":false,"user":{"id":3,"email":"root@root.de","vorname":"root","nachname":"toor","wka":"wka1"}}
我正在使用Newtonsoft对响应进行反序列化,我认为问题在于用户{…}后面的花括号,因为我可以打印出公共bool错误{get;set;},但其他变量不起作用

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; }
    }
测试:

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

用于JSON的C类不正确

应该是

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

然后您可以将JSON反序列化为C对象

您可以使用一些JSON-to-C转换器来获取模型,即。当您需要获取大型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; }
}

这门课错了。将JSON复制到剪贴板,并使用编辑菜单>粘贴特殊>粘贴JSON作为类。请阅读并获取以下信息:用户是响应中的一个对象,id是用户对象的属性,而不是基本响应对象。使用类似的工具帮助为您的数据生成适当的C类只有两个属性,错误:bool和一个字典由于响应速度快,json2csharp.com的解决方案正在运行