如何使用newtonsoft反序列化json字符串

时间:2017-08-30 06:35:23

标签: json xamarin.android

你好我发送请求到服务器,我想使用newtonsoft

反序列化json字符串

这是我的代码

  public static async Task CallWebAPIAsync()
    {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://52.163.215.46:4444/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //POST Method
                VMLogin data = new VMLogin() { Id = 1, NAME = "abhijit22", PROF_EMAILD = "abhijit@gmail.com", MAILID = "abhijit@gmail.com" };

                    var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
                    var responsePost = client.PostAsync("api/FBLogins", content);
                    if (responsePost.Result.IsSuccessStatusCode)
                    {
                        // Get the URI of the created resource.
                        Uri returnUrl = responsePost.Result.Headers.Location;
                        Console.WriteLine(returnUrl);

                //how to deserialize json string
            }
        }
    }
public class VMLogin
    {
        public int Id { get; set; }
        public string NAME { get; set; }
        public string PROF_EMAILD { get; set; }
        public string MAILID { get; set; }
    }

    public class LoginResponse
    {
        public string status { get; set; }
    }

如何反序列化我从服务器获得的json字符串......

1 个答案:

答案 0 :(得分:0)

这是将JSON反序列化为类模型的行(我假设响应的模型是LoginResponse)

LoginResponse response = JsonConvert.DeserializeObject<LoginResponse>(json_str);

json_str 是您从服务器获取的json字符串响应