简体   繁体   English

如何在C#中反序列化JSON数组(或列表)

[英]How to Deserialize JSON array(or list) in C#

public static string DeserializeNames()
{
    // Json I am passing for the deserialization.
    JsonStream= "{
    "head": {

        "Rows": [
            "test 1",
            [
                [
                    {
                        "@Key": "Domain",
                        "@value": "LocalHost"
                    },
                    {
                        "@Key": "Cookie name(s)",
                        "@value": "Google"
                    },
                    {
                        "@Key": "Purpose",
                        "@value": "Test"
                    },
                    {
                        "@Key": "lifetime",
                        "@value": "test"
                    }
                ]
                ]
]
}
}"


        //deserialize JSON from file  
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        var cookieList = serializer.Deserialize<List<Cookie>>(JsonStream).ToList();
}

//Class descriptions //I have created below classes for the deserialization. //类说明//我在下面的类中创建了反序列化。 Records are not deserialized i am getting zero record count. 记录未反序列化,记录计数为零。

public class Categorization
        {
            public string categorizationName { get; set; }
            public List<object> KeyValue{ get; set; }
        }
        public class Head
        {
            public IList<Categorization> Rows { get; set; }
        }

        public class Cookie
        {
            public Head head { get; set; }
        }

Also created below set of the classes and tried the deserialization, Still no luck 还创建了下面的一组类并尝试了反序列化,仍然没有运气

public class Head 
{
    public List<object> Rows { get; set; }
}

public class Cookie
{
    public Head head { get; set; }
}

I am getting count as 0 i am not able to fetch any record. 我被计数为0,我无法获取任何记录。

Please help !! 请帮忙 !!

I have modified the Json as below and stored in in the file "test.json" : 我已经如下修改了Json并将其存储在文件“ test.json”中:

  {
    "head": {

            "TestRows": [

                [
                    {
                        "Key": "Domain",
                        "value": "Google"
                    },
                    {
                        "Key": "Cookie",
                        "value": "Test"
                    },
                    {
                        "Key": "Use for",
                        "value": "Test."
                    },
                    {
                        "Key": "Expire Time",
                        "value": "1 hour"
                    }
                  ]
                ]
    }
}

And created below set of classes : 并创建以下类集:

public class Head
        {
            public IList<List<Dictionary<string, object>>> TestRows{ get; set; }
        }

        public class Cookie
        {
            public Head Head { get; set; }
        }




var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var path= Path.Combine(baseDirectory, "test.json");

            //deserialize JSON from file  
            string JsonStream = System.IO.File.ReadAllText(path, Encoding.Default);

            var DeserializedCookieList = JsonConvert.DeserializeObject<Cookie>(JsonStream);

Deserialization is working properly. 反序列化工作正常。

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

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