Преобразовать вложенный JSON в словарь - PullRequest
0 голосов
/ 27 марта 2019

У меня есть вложенный 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; }
}
...