简体   繁体   English

如何在Windows Phone 8.1中反序列化json对象?

[英]How to Deserialize the json object in windows phone 8.1?

I want to bind json obect to my properties When I deserialize the json object and bind into the properties ,properties shows null values,please some one help me to resolve this. 我想将json obect绑定到我的属性中当我反序列化json对象并绑定到属性中时,properties显示空值,请有人帮我解决这个问题。

this is my code 这是我的代码

string s = "{\"response\":{\"status\":\"fail\",\"content\":{\"user_id\":\"56\",\"first\":\"kiran\",\"last\":\"kumar\",\"username\":\"kirankumar\"},\"msg\":\"shggh\"}}";

var jsonObj = JObject.Parse(s);

response myDeserializedObj = (response)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonObj.ToString(), typeof(response));

this is properties 这是属性

public class response
{
    public string status { get; set; }
    public content content { get; set; }
}

public class content
{
    public string user_id { get; set; }
    public string first { get; set; }
    public string last { get; set; }
    public string username { set; get; }
}

Thanks, karthik 谢谢,karthik

I copied you code and tested it in my machine and I could solve your problem here is the solution 我复制了代码并在我的机器上对其进行了测试,可以解决您的问题,这是解决方案

add the following class 添加以下类

public class ResponseWrapper
{
    public response response { get; set; }
}

replace your code with the following 用以下代码替换您的代码

string s = "{\"response\":{\"status\":\"fail\",\"content\":{\"user_id\":\"56\",\"first\":\"kiran\",\"last\":\"kumar\",\"username\":\"kirankumar\"},\"msg\":\"shggh\"}}";

response my = JsonConvert.DeserializeObject<ResponseWrapper>(s).response;

I am sure this will work. 我相信这会起作用。

UPDATE 更新

Another solution (Which is tested also) and better than the first one because it is more clean, and in this way you have not to create new wrapper class. 另一个解决方案(也经过测试)也比第一个解决方案 ,因为它更干净,而且您不必创建新的包装器类。

the solution is replace your string with the following string. 解决方案是将您的字符串替换为以下字符串。

and all of your previous code will stay the same here is the correct JSON string 并且您之前的所有代码都将保持不变,这里是正确的JSON字符串

string s = "{\"status\":\"fail\",\"content\":{\"user_id\":\"56\",\"first\":\"kiran\",\"last\":\"kumar\",\"username\":\"kirankumar\"},\"msg\":\"shggh\"}";

UPDATE 2 更新2

in the comments below of this answer you asked a completely a new question. 在此答案下面的评论中,您提出了一个全新的问题。 here is your new question (I copied this from your comments) 这是您的新问题(我从您的评论中复制了此问题)

public class responseWraper
{
    public response response { get; set; }
}

public class response
{
    public string status { get; set; }
    public content content { get; set; }
}

public class content
{
    public Employees Employees { get; set; }
}

public class Employees
{
    public string Employee_id { get; set; }
    public string Employee_name { get; set; }
    public string status { get; set; }
}      

and here is how you are trying to deserialize this (also this copied from your comments) 这是您尝试反序列化的方式(也是从您的评论中复制的内容)

string s = "{\"response\":{\"status\":\"success\",\"content\":{\"Employees\":[{\"Employee_i‌​d\":\"1\",\"Employee_name\":\"Sravan\",\"status\":\"1\"},}]}}}"; 
response my = Newtonsoft.Json.JsonConvert.DeserializeObject<responseWraper>(s).response;

ANSWER your code has two problem 回答您的代码有两个问题

the first is you are using the Employees as array in the JSON string, but the type of the Employees property is not an array 首先是您在JSON字符串中使用Employees as数组,但是Employees属性的类型不是数组

the second problem that the JSON string itself is not valid. 第二个问题是JSON字符串本身无效。 it has an errors there is 4 { character but you have 5 } character inside it. 它有一个错误,里面有4个{字符,但您有5个}字符。

so you have to fix those two problem as the following 所以您必须按照以下步骤解决这两个问题

public class content
{
    public List<Employees> Employees { get; set; }
}

and the string is 字符串是

 string s = "{\"response\":{\"status\":\"success\",\"content\":{\"Employees\":[{\"Employee_id\":\"1\",\"Employee_name\":\"Sravan\",\"status\":\"1\"},]}}}";

在此处输入图片说明

and if you have any other question , I will be happy to help you :) 如果您还有其他问题,我们将很乐意为您提供帮助:)

Try your JSON with http://json2csharp.com/ . 使用http://json2csharp.com/尝试JSON。 Your current code would deserialize the following JSON: 您当前的代码将反序列化以下JSON:

{  
   "status":"fail",
   "content":{  
      "user_id":"56",
      "first":"kiran",
      "last":"kumar",
      "username":"kirankumar"
   },
   "msg":"shggh"
}

You are missing a root class with a single Property "response" with the type "Response" 您缺少带有单个属性“ response”且类型为“ Response”的根类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM