反序列化json C# [英] Deserialising json C#

查看:51
本文介绍了反序列化json C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的json如下:

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

使用 http://json2csharp.com/我对其进行反序列化,如下所示:

Using http://json2csharp.com/ I am deserialising it as below:

 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是我的名称空间名称.如何获取实际数据?

The value of results is {Test.Info[0]} where Test is my namespace name. How do I obtain the actual data?

推荐答案

您的Info类应该是

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

这篇关于反序列化json C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆