简体   繁体   English

无法将JSON反序列化为C#4.0中的列表

[英]Unable to deserialize JSON into List in C# 4.0

I am trying to deserialize JSON into List. 我正在尝试将JSON反序列化为List。 I have .NET 4.0. 我有.NET 4.0。 Normal deserialization into an object works fine. 将正常反序列化为对象可以正常工作。 I am an absolute beginner in C#. 我绝对是C#的初学者。

I wrote the following code with the help from various forums. 我在各个论坛的帮助下编写了以下代码。 When I am trying to load the deserialized JSON into a List for Lad type, it doesn't give any error but when I check Lads object it's just blank. 当我尝试将反序列化的JSON加载到Lad类型的列表中时,它没有给出任何错误,但是当我检查Lads对象时,它只是空白。

using System;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace RESTServicesJSONParserExample
{
    public class MyDate
    {
        public int year { get; set; }
        public int month { get; set; }
        public int day { get; set; }
    }

    public class Lad
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public MyDate dateOfBirth { get; set; }
    }

    class Program
    {
        static void Main()
        {
            var obj = new Lad
            {
                firstName = "Markoff",
                lastName = "Chaney",
                dateOfBirth = new MyDate
                {
                    year = 1901,
                    month = 4,
                    day = 30
                }

            };
            var json = new JavaScriptSerializer().Serialize(obj);
            Console.WriteLine(json);


            //This fails to load desereialized JSON in the List
            List<Lad> Lads = new JavaScriptSerializer().Deserialize<List<Lad>>(json);
            Lad Lad1 = new JavaScriptSerializer().Deserialize<Lad>(json);

            Console.WriteLine(Lad1.firstName);


            //Nothing returned here
            foreach (Lad Lad2 in Lads)
            {
                Console.WriteLine(Lad2.firstName);
            }

            Console.ReadLine();
        }
    }
}

You are serializing a single Lad . 您正在序列化一个Lad You can't deserialize it as a collection of Lad s! 您不能将其反序列化为Lad的集合!

You could change the code in: 您可以在以下位置更改代码:

var obj = new List<Lad>
{ 
    new Lad
    {
        firstName = "Markoff",
        lastName = "Chaney",
        dateOfBirth = new MyDate
        {
            year = 1901,
            month = 4,
            day = 30
        }
    } 
};

Now Lads will have one Lad , while Lad1 will crash (because you can't deserialize a collection as a single element). 现在Lads将有一个Lad ,而Lad1会崩溃(因为你不能反序列化的集合作为一个单一的元素)。

Just as a curiosity, your "original" json was 出于好奇,您的“原始” json是

{"firstName":"Markoff","lastName":"Chaney","dateOfBirth":{"year":1901,"month":4,"day":30}}

Introducing the new List<> it becomes: 引入new List<>它将变为:

[{"firstName":"Markoff","lastName":"Chaney","dateOfBirth":{"year":1901,"month":4,"day":30}}]

Se the initial and final [] ? 硒的最初和最后[] Now there is an array of objects in the json object! 现在json对象中有一个对象数组!

I actually just had to do this yesterday and this is how I did it (not sure if there's an easier way, but it worked for me). 我实际上只是昨天要做的,这就是我的操作方式(不确定是否有更简单的方法,但这对我有用)。 I deserialized each object and added that object to my List. 我反序列化每个对象并将该对象添加到我的列表中。 I had a list of json objects (generic objects) so I looped through them with a foreach, adding them to the List. 我有一个json对象(通用对象)列表,所以我用foreach遍历了它们,并将它们添加到列表中。 Also, I use JsonConvert.DeserializeObject, but I believe it should work the same way.Try this though: 此外,我使用JsonConvert.DeserializeObject,但我相信它应该以相同的方式工作。

List<Lad> Lads = new List<Lad>();

Lads.Add(new JavaScriptSerializer().Deserialize<Lad>(json));

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

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