序列化为JSON异常"类型需要JSON数组"

时间:2016-09-02 03:19:32

标签: c# json serialization

我想尝试使用JSON.Net序列化数据,以节省游戏进度和简化测试。 我不断得到一个例外,那就是:

  

附加信息:无法将当前JSON对象(例如{" name":" value"})反序列化为类型System.Collections.Generic.List`1 [ JSONDataStorage.Sword]'因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。

我对造成这种异常的原因完全不了解。

看看我的代码:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;

class Sword : IEquatable<Sword>
{
    public string name;
    public int minimumDamage;
    public int maximumDamage; 

    public Sword(string name, int minimumDamage = 0, int maximumDamage = 0)
    {
        this.name = name;
        this.minimumDamage = minimumDamage;
        this.maximumDamage = maximumDamage;
    }

    public bool Equals(Sword other)
    {
        if (other == null) return false;
        return (this.name.Equals(other.name));

    }
}

class Program
{
    static void Main(string[]args)
    {
        // Read file into a string and deserialize JSON to a type
        Console.WriteLine("Reading data.json...");
        string jsonSTRING = File.ReadAllText("data.json");
        List<Sword> myList = JsonConvert.DeserializeObject<List<Sword>>(jsonSTRING);

        if (myList == null)
            myList = new List<Sword>();

        string input = "";
        int inputMinimumDamage = 0;
        int inputMaximumDamage = 0;
        string inputName = "";

        while (input != "q")
        {
            Console.WriteLine("Press 'a' to Add new Item");
            Console.WriteLine("Press 'd' to Delete Item");
            Console.WriteLine("Press 's' to Show Content");
            Console.WriteLine("Press 'q' to quit Program");
            Console.Write("Press Command: ");
            input = Console.ReadLine();
            switch (input) // Switch on input string
            {
                case "a":
                    Console.WriteLine("Adding a new Item...");
                    Console.Write("Enter item name: ");
                    inputName = Console.ReadLine();
                    Console.WriteLine("Enter item Minimum Damage value: ");
                    inputMinimumDamage = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter item Minimum Damage value: ");
                    inputMaximumDamage = Convert.ToInt32(Console.ReadLine());
                    myList.Add(new Sword(inputName, inputMinimumDamage, inputMaximumDamage));
                    Console.WriteLine("Sword: " + inputName + "\nDamage Stats: " + inputMinimumDamage + "/" + inputMaximumDamage);
                    break;
                case "d":
                    Console.WriteLine("Deleting item");
                    Console.WriteLine("Enter item name to delete");
                    inputName = Console.ReadLine();
                    myList.Remove(new Sword(inputName));
                    Console.WriteLine("Deleted item with name" + inputName);
                    break;
                case "q":
                    Console.WriteLine("Quitting program");
                    break;
                case "s":
                    Console.WriteLine("Showing Contents");
                    foreach (Sword item in myList)
                    {
                        Console.WriteLine("Sword: " + item.name + "\nStats: " + inputMinimumDamage + "/" + inputMaximumDamage);
                    }
                    Console.WriteLine();
                    break;
                default:
                    Console.WriteLine("Incorrect command, try again");
                    break;
            }
        }

        Console.WriteLine("Rewriting data.json");
        string data = JsonConvert.SerializeObject(myList);
        File.WriteAllText("data.json", data);
        Console.ReadLine();
    }
}

当我将列表反序列化为JSON格式时,我收到此错误。那就是那个,谢谢!

0 个答案:

没有答案