JavascriptSerializer无法反序列化对象

时间:2016-03-18 00:39:34

标签: c# json javascriptserializer

我在C#中构建项目。

因此我使用名为" Account"

的对象

我将其保存为Json格式。

到目前为止,我已经能够使用它了一点点,它很好。 当我到达这里的代码时突然间:

private void ReadAccountsData()
{

           string[] files = Directory.GetFiles(_pathAccount, "*.json");
           string text;
           foreach (string json in files)
           {

               text = System.IO.File.ReadAllText(json);
               JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(new SimpleTypeResolver());
                   // problem!
               Account dto = (Account)jsonSerializer.Deserialize(text, typeof(Account)); // ISSUE


           }

}

它说:

An exception of type 'System.MissingMethodException' occurred in mscorlib.dll but was not handled in user code

Additional information: No parameterless constructor defined for this object.

我删除并清理了所有的dll文件,但它没有用。

//帐户类:

using ATMInfra.interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATMInfra
{
    [Serializable]
    public class Account:IAccount
    {
        List<ClientPersonalDetailsDTO> _owners;

        public List<ClientPersonalDetailsDTO> Owners
        {
            get { return _owners; }
            set { _owners = value; }
        }

        Dictionary<string, IPakam> _pakams;

        public Dictionary<string, IPakam> Pakams
        {
            get { return _pakams; }
            set { _pakams = value; }
        }
        Dictionary<string, ITransaction> _transactions;

        public Dictionary<string, ITransaction> Transactions
        {
            get { return _transactions; }
            set { _transactions = value; }
        }
        private float _balance;

        public float Balance
        {
            get { return _balance; }
            set { _balance = value; }
        }


        private int _account_number = -1;

        public int Account_number
        {
            get { return _account_number; }
            set { _account_number = value; }
        }

        private int _pakamNumber = 0;

        public int PakamNumber
        {
            get { return _pakamNumber; }
            set { _pakamNumber = value; }
        }


        public Account()
        {

            _balance = 0;
            // pakam name + implemantaion
            _pakams = new Dictionary<string, IPakam>();
            _transactions = new Dictionary<string, ITransaction>();
            _owners = new List<ClientPersonalDetailsDTO>();
        }



        public void AddOwners(ClientPersonalDetailsDTO owner)
        {
            _owners.Add(owner);
        }
        public void AddToBalance(float amount)
        {
            _balance += amount;
        }

        public bool CanWithDraw(float amount)
        {
            return amount <= Balance;
        }

        public void WithDraw(float amount)
        {
            _balance -= amount;
        }


        public bool AddPakam(IPakam pkm)
        {
            if (pkm.Amount > _balance)
            {
                return false;
            }
            _pakams.Add("" +_pakamNumber++, pkm);
            _balance -= pkm.Amount;
            return true;

        }

        public void AddTransaction(ITransaction transaction)
        {
            string nameDate = transaction.Name +" "+ DateTime.Now.ToShortDateString();
            _transactions.Add(nameDate, transaction);
            //_transactions[nameDate] = transaction;
        }

        public void RemovePakam(IPakam pkm)
        {
            if (_pakams.ContainsKey(pkm.PakamID))
            {
                _pakams.Remove(pkm.PakamID);
                _balance += pkm.Amount;
            }
        }

        public void RemoveTransaction(ITransaction transaction)
        {
            if (_transactions.ContainsKey(transaction.Name))
            {
                _transactions.Remove(transaction.Name);
            }
        }

        public Dictionary<string, ITransaction> GetTransactions()
        {
            return _transactions;
        }

        public int GetAccountNumber()
        {
            return _account_number;
        }


        public float GetBalance()
        {
            return _balance;
        }


        public Dictionary<string, IPakam> GetPakams()
        {
            return _pakams;
        }


        public int GeneratePakamID()
        {
            return _pakamNumber;
        }

        public override string ToString()
        {
            Console.WriteLine("Clients details : ");
            foreach (ClientPersonalDetailsDTO client in _owners)
            {
                Console.WriteLine(client);
            }
            return "\nAccount : " + Account_number + "\n-current balance : " + Balance + "\n--Number of transactions: " + Transactions.Count + "\n---Number of Pakams: " + Pakams.Count + " Pakams";
        }

        public Account GetAccoutJson()
        {
            return this;
        }


        public void AddAccountNumber(int accountNumber)
        {
            Account_number = accountNumber;
        }
    }
}

// JSON帐户示例

{"__type":"ATMInfra.Account, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","Owners":[{"__type":"ATMInfra.ClientPersonalDetailsDTO, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","FirstName":"1","LastName":"1","ID":"1","Address":{"__type":"ATMInfra.Address, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","City":"1","Street":"1","StreetNumber":1},"UserNAme":"1","Password":"1","AccountNumber":"0"}],"Pakams":{"0":{"__type":"ATMInfra.PakamDaily, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458258314948)\/","Amount":200,"Name":"Open Pakam transaction","PakamID":"0","BeginDate":"\/Date(1458258314948)\/","EndDate":"\/Date(1458344714948)\/","ProfitPercentage":2}},"Transactions":{"Diposit transaction via: check18-Mar-16":{"__type":"ATMInfra.DipositTransaction, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458257731695)\/","Amount":2000.25,"Name":"Diposit transaction via: check"},"Withdraw transaction18-Mar-16":{"__type":"ATMInfra.WithDrawTransaction, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458257625563)\/","Amount":1,"Name":"Withdraw transaction"},"Fail to Withdraw transaction18-Mar-16":{"__type":"ATMInfra.WithDrawTransaction, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458257693742)\/","Amount":2000,"Name":"Fail to Withdraw transaction"},"Diposit transaction via: cash18-Mar-16":{"__type":"ATMInfra.DipositTransaction, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458255736255)\/","Amount":1,"Name":"Diposit transaction via: cash"},"Withdraw transaction 18-Mar-16":{"__type":"ATMInfra.WithDrawTransaction, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458257848390)\/","Amount":2000,"Name":"Withdraw transaction"},"Fail to Withdraw transaction 18-Mar-16":{"__type":"ATMInfra.WithDrawTransaction, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458257940695)\/","Amount":2000,"Name":"Fail to Withdraw transaction"},"Diposit transaction via: check 18-Mar-16":{"__type":"ATMInfra.DipositTransaction, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458258048571)\/","Amount":800,"Name":"Diposit transaction via: check"},"Open Pakam transaction 18-Mar-16":{"__type":"ATMInfra.PakamDaily, ATMInfra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IDExecuter":"1","Date":"\/Date(1458258314948)\/","Amount":200,"Name":"Open Pakam transaction","PakamID":"0","BeginDate":"\/Date(1458258314948)\/","EndDate":"\/Date(1458344714948)\/","ProfitPercentage":2}},"Balance":820.1699,"Account_number":0,"PakamNumber":1}

任何想法?

由于

1 个答案:

答案 0 :(得分:1)

错误为您提供详细信息:没有为此对象定义无参数构造函数。

这意味着它正在寻找Account类的构造函数,该构造函数不接受任何参数(即new Account();)。 Account中是否存在无参数构造函数?