如何在C#

时间:2018-04-13 06:22:37

标签: c# json unicode character-encoding json-deserialization

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

//班级说明 //我为反序列化创建了以下类。记录未反序列化我记录的数量为零。

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

还在类集下创建并尝试反序列化,仍然没有运气

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

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

我计数为0我无法获取任何记录。

请帮忙!!

1 个答案:

答案 0 :(得分:0)

我已经如下修改了Json,并将其存储在“test.json”文件中:

  {
    "head": {

            "TestRows": [

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

在下面的一组课程中创建:

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

反序列化工作正常。