本文介绍了反序列化JSON数组(或列表)在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是基本的code:

here is the basic code:

public static string DeserializeNames()
{

    jsonData = "{\"name\":[{\"last\":\"Smith\"},{\"last\":\"Doe\"}]}";

    JavaScriptSerializer ser = new JavaScriptSerializer();

    nameList myNames = ser.Deserialize<nameList>(jsonData);

    return ser.Serialize(myNames);
}

//Class descriptions

public class name
{

   public string last { get; set; }

}

public class nameList
{

   public List<name> { get; set; }

}

我曾尝试:

nameList myNames = ser.Deserialize<nameList>(jsonData);

List<name> myNames = ser.Deserialize<List<name>>(jsonData);

和既不工作。任何人有什么想法?

and neither work. Anyone have any ideas?

推荐答案

这code对我的作品:

This code works for me:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace Json
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DeserializeNames());
            Console.ReadLine();
        }

        public static string DeserializeNames()
        {
            var jsonData = "{\"name\":[{\"last\":\"Smith\"},{\"last\":\"Doe\"}]}";

            JavaScriptSerializer ser = new JavaScriptSerializer();

            nameList myNames = ser.Deserialize<nameList>(jsonData);

            return ser.Serialize(myNames);
        }

        //Class descriptions

        public class name
        {
            public string last { get; set; }
        }

        public class nameList
        {
            public List<name> name { get; set; }
        }
    }
}

这篇关于反序列化JSON数组(或列表)在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:05