1

In a VB.Net application (v4.0 of the .Net Framework) I am making a call to an API that returns the following:

{"CFResponse":{"AccountDetails":[{"AccountNum":"TEST","CurrentBalance":-58.24,"OriginalBalance":1530.01,"TotalPaid":215.33}]}}

I also have a variable (userInfo) holding an instance of GetUserInfoResult.vb that has a bunch of set properties from previous logic.

What is a clean way of getting the JSON data mapped to properties on my instance?

CurrentBalance > userInfo.Balances.CurrentBalance

OriginalBalance > userInfo.Balances.OriginalBalance

TotalPaid > userInfo.Balances.TotalPaid

5
  • Possible duplicate of Deserializing JSON in Visual basic Commented Sep 6, 2017 at 8:05
  • Or this or this maybe or if you don't mind converting then this Commented Sep 6, 2017 at 8:07
  • @AFriend If I were to use JsonConvert.DeserializeObject(Of GetUserInfoResult)(jsonString) would I not lose the other properties already set? Or are you saying I should create a Dto class that maps exactly to the JSON structure? But if I do that, it seems tedious, cause the returned JSON is quite messy when I only need them three properties! Commented Sep 6, 2017 at 8:11
  • Well you can Deserialise to a temporary object, set the 3 properties you need, and then get rid of the temporary one. Also, as long as the property names match, you shouldn't need the JSON attributes to deserialise properly Commented Sep 6, 2017 at 8:21
  • hmm, can you show me how please in an answer? Commented Sep 6, 2017 at 8:22

1 Answer 1

1

You'll need to make some changes but the rough idea is here

Private Sub SomeJsonThing()
    Dim json = "{'cFResponse': {'AccountDetails':[{'AccountNum':'TEST','CurrentBalance':-58.24,'OriginalBalance':1530.01,'TotalPaid':215.33}]}}"

    Dim tmpJObject As JObject = JsonConvert.DeserializeObject(json)
    Dim cfResponse As JToken = tmpJObject("CFResponse")
    Dim accDetails As JToken = cfResponse("AccountDetails")

    Dim accountDetailsList = accDetails.ToObject(Of List(Of AccountDetails))
    Dim accInfo = accountDetailsList.First

    userInfo.Balances.CurrentBalance = accInfo.CurrentBalance
    userInfo.Balances.OriginalBalance = accInfo.OriginalBalance
    userInfo.Balances.TotalPaid = accInfo.TotalPaid
End Sub

Public Class AccountDetails
    Property AccountNum As String
    Property CurrentBalance As Double
    Property OriginalBalance As Double
    Property TotalPaid As Double
End Class

I'd separate the JSON reading into a different method, and the setting of the properties as well.

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

2 Comments

Am I right that I first need to do Install-Package Newtonsoft.Json -Version 10.0.3?
Whichever compatible version, yes. If you prefer not to install the package then this link I provided has a JavascriptSerializer method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.