将嵌套的JSON转换为字典<string,string>

时间:2019-03-27 14:44:38

标签: c# json json-deserialization

我有一个嵌套的JSON,需要将其转换为字典。我收到如下错误。

将值“ 12345”转换为“ System.Collections.Generic.IDictionary`2 [System.String,System.String]”时出错。路径“ requestId”,第2行,位置24。

这是我尝试过的代码。

public static void LoadJson()
{
    using (StreamReader r = new StreamReader("D:SampleJson.json"))
    {
    string json = r.ReadToEnd();              
    var mergeCollection =
                    JsonConvert.DeserializeObject<IDictionary<string, IDictionary<string, string>>>(json);

    }
}

JSON字符串是

{
    "requestId": "12345",
    "financials": {
        "accountFee": 1234.45,
        "dailyAmount": 1234.45,
        "redemptionAmount": 1234.45
    },
    "sundry": {
        "arrearsBalance": 1234.45,
        "unclearAmount": 1234.45,
        "interestAmount": 1234.45,
        "dailyInterestAmount": 1.5
    },
    "savings": {
        "capitalBalance": 1234.45,
        "unclearAmount": 1234.45
    },
    "overpayments": {
        "capitalBalance": 1234.45,
        "unclearAmount": 1234.45
    },
    "availabeFunds": {
        "capitalBalance": 1234.45,
        "arrearsBalance": 1234.45,
        "unclearAmount": 1234.45,
        "interestAmount": 1234.45,
        "feeAmount": 1234.45,
        "dailyInterestAmount": 1.5
    },
    "loans": [
        {
            "lsRate": 1234.45,
            "capitalBalance": 1234.45,
            "arrearsBalance": 1234.45,
            "unclearAmount": 1234.45,
            "interestAmount": 1234.45,
            "feeAmount": 1234.45,
            "dailyInterestAmount": 1.5
        },
        {
            "lsRate": 1234.45,
            "capitalBalance": 1234.45,
            "arrearsBalance": 1234.45,
            "unclearAmount": 1234.45,
            "interestAmount": 1234.45,
            "feeAmount": 1234.45,
            "dailyInterestAmount": 1.5
        }
    ],
    "cashbackCharges": [
        {
            "description": "some charge type",
            "feeAmount": 1234.45
        },
        {
            "description": "some charge type",
            "feeAmount": 1234.45
        }
    ],
    "statementText": [
        {
            "sequence": 1,
            "text": "The information below shows the outstanding capital and overdue balances for each loan of the mortgage account (loan scheme).  It also details the expected charges for each loan scheme up to the redemption date. The figure assumes that no further credits to the account will be received."
        },
        {
            "sequence": 2,
            "text": "The amount needed to pay back (redeem) the mortgage will change if there are any unpaid cheques, recalled Direct Debits (which have been used in the calculation), interest rate changes, and/or extra charges.  In the case of a Flexible or Flexible Offset mortgage, if money is taken from the Available Funds (and/or withdrawal of savings in the case of a Flexible Offset mortgage) after the date of issue, the amount will also change."
        }        
    ]
}

然后按如下所示生成类文件

public class RedemptionInfo
{
    public string requestId { get; set; }
    public Financials financials { get; set; }
    public Sundry sundry { get; set; }
    public Savings savings { get; set; }
    public Overpayments overpayments { get; set; }
    public AvailabeFunds availabeFunds { get; set; }
    public List<Loan> loans { get; set; }
    public List<CashbackCharge> cashbackCharges { get; set; }
    public List<StatementText> statementText { get; set; }
}

public class Financials
{
    public string accountFee { get; set; }
    public string dailyAmount { get; set; }
    public string redemptionAmount { get; set; }
}

public class Sundry
{
    public string arrearsBalance { get; set; }
    public string unclearAmount { get; set; }
    public string interestAmount { get; set; }
    public string dailyInterestAmount { get; set; }
}

public class Savings
{
    public string capitalBalance { get; set; }
    public string unclearAmount { get; set; }
}

public class Overpayments
{
    public string capitalBalance { get; set; }
    public string unclearAmount { get; set; }
}

public class AvailabeFunds
{
    public string capitalBalance { get; set; }
    public string arrearsBalance { get; set; }
    public string unclearAmount { get; set; }
    public string interestAmount { get; set; }
    public string feeAmount { get; set; }
    public string dailyInterestAmount { get; set; }
}

public class Loan
{
    public string lsRate { get; set; }
    public string capitalBalance { get; set; }
    public string arrearsBalance { get; set; }
    public string unclearAmount { get; set; }
    public string interestAmount { get; set; }
    public string feeAmount { get; set; }
    public string dailyInterestAmount { get; set; }
}
public class CashbackCharge
{
    public string description { get; set; }
    public string feeAmount { get; set; }
}

public class StatementText
{
    public int sequence { get; set; }
    public string text { get; set; }
}

0 个答案:

没有答案
相关问题