0

I want to read a data from Json Response But i am getting Difficulty to get the data. i want to read this item from Json Response as Shown Below

period, fromDate, dueDate, totalInstallmentAmountForPeriod

Json Response

{
        "currency": {
        "decimalPlaces": 0,
        "inMultiplesOf": 1
    },
    "loanTermInDays": 92,
    "totalPrincipalDisbursed": 100000,
    "periods": [
        {
            "dueDate": [
                2022,
                7,
                8
            ],
            "principalDisbursed": 100000,
            "totalActualCostOfLoanForPeriod": 0
        },
        {
            "period": 1,
            "fromDate": [
                2022,
                7,
                8
            ],
            "dueDate": [
                2022,
                8,
                8
            ],
            "daysInPeriod": 31,
            "totalInstallmentAmountForPeriod": 36035
        },
        {
            "period": 2,
            "fromDate": [
                2022,
                8,
                8
            ],
            "dueDate": [
                2022,
                9,
                8
            ],
            "daysInPeriod": 31,
            "totalInstallmentAmountForPeriod": 36035
        }
    ],
    "mandatorySavings": [
        { "periodId": 0"expectedSavings": 10000.000}]
}
3
  • You can use the Paste Special option to create a Class that you can then deserialize the json into. Commented Jul 8, 2022 at 13:22
  • @Anu6is but how can i achive this in vb.net Commented Jul 8, 2022 at 13:29
  • Here's another option: JSON Utils Commented Jul 8, 2022 at 15:04

1 Answer 1

1

Your JSON is invalid, it looks like there is a missing comma after mandatorySavings -> periodId and expectedSavings.

Visual Studio has a cool feature called Paste JSON as Classes that can be found under Edit > Paste Special > Paste JSON as Classes. Using either Newtonsoft.Json or System.Text.Json, you can tidy the classes up a little bit using decorators so that you can conform to .NET standard naming conventions while still serializing the JSON to the expected values. I also prefer to use IEnumerables over arrays, but the paste JSON as classes uses the latter.

This is how the class definitions would look tidied up a bit:

Public Class Rootobject
    <JsonProperty("currency")>
    Public Property Currency As Currency

    <JsonProperty("loanTermInDays")>
    Public Property LoanTermInDays As Integer

    <JsonProperty("totalPrincipalDisbursed")>
    Public Property TotalPrincipalDisbursed As Integer

    <JsonProperty("periods")>
    Public Property Periods As IEnumerable(Of Period)

    <JsonProperty("mandatorySavings")>
    Public Property MandatorySavings As IEnumerable(Of Mandatorysaving)

End Class

Public Class Currency

    <JsonProperty("decimalPlaces")>
    Public Property DecimalPlaces As Integer

    <JsonProperty("inMultiplesOf")>
    Public Property InMultiplesOf As Integer

End Class

Public Class Period

    <JsonProperty("dueDate")>
    Public Property DueDate As IEnumerable(Of Integer)

    <JsonProperty("principalDisbursed")>
    Public Property PrincipalDisbursed As Integer

    <JsonProperty("totalActualCostOfLoanForPeriod")>
    Public Property TotalActualCostOfLoanForPeriod As Integer

    <JsonProperty("period")>
    Public Property Period As Integer

    <JsonProperty("fromDate")>
    Public Property FromDate As IEnumerable(Of Integer)

    <JsonProperty("daysInPeriod")>
    Public Property DaysInPeriod As Integer

    <JsonProperty("totalInstallmentAmountForPeriod")>
    Public Property TotalInstallmentAmountForPeriod As Integer

End Class

Public Class Mandatorysaving

    <JsonProperty("periodId")>
    Public Property PeriodId As Integer

    <JsonProperty("expectedSavings")>
    Public Property ExpectedSavings As Integer

End Class

Now all you would need to do is use JsonConvert.DeserializeObject to convert the JSON literal to an instance of your Rootobject class:

Dim conversion = JsonConvert.DeserializeObject(Of Rootobject)(literal)

Fiddle: https://dotnetfiddle.net/Eq88rV

Sign up to request clarification or add additional context in comments.

2 Comments

how can i access this data it is in array period, fromDate, dueDate, totalInstallmentAmountForPeriod.
@HuzefaSayed - how much experience do you have in VB.NET?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.