反序列化json C#

时间:2017-04-26 10:52:29

标签: c# json

我有json如下:

{"data":[{"name":"123","pwd":123},{"name":"456","pwd":456},{"name":"789","pwd":789}],"duration":5309,"query":"myquery","timeout":300}

使用http://json2csharp.com/我将其反序列化如下:

 namespace Test 
   {
    public class Info
    {
        public string name{ get; set; }
        public string pwd{ get; set; }
    }

    public class Product
    {
        public Info[] data { get; set; }
        public int duration { get; set; }
        public string query { get; set; }
        public int timeout { get; set; }
    }
   //code here, function start etc.

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new 
    StreamReader(httpResponse.GetResponseStream()))
     {
            var result = streamReader.ReadToEnd();
            Product myprod = JsonConvert.DeserializeObject<Product>(result); 
            var results = myprod.data;
     }
}

结果的值是{Test.Info [0]},其中Test是我的命名空间名称。我如何获得实际数据?

1 个答案:

答案 0 :(得分:0)

您的信息类应该是

public class Info
    {
        public string name { get; set; }
        public int pwd { get; set; }
    }

这应该有效

var testJson = "{\"data\":[{\"name\":\"123\",\"pwd\":123},{\"name\":\"456\",\"pwd\":456},{\"name\":\"789\",\"pwd\":789}],\"duration\":5309,\"query\":\"myquery\",\"timeout\":300}";

        var product = JsonConvert.DeserializeObject<Product>(testJson);