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

halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228
  • Possible duplicate of [Deserializing JSON in Visual basic](https://stackoverflow.com/questions/20079177/deserializing-json-in-visual-basic) – A Friend Sep 06 '17 at 08:05
  • Or [this](https://stackoverflow.com/questions/8118019/vb-net-json-deserialize) or [this maybe](https://stackoverflow.com/questions/7406948/deserialize-json-into-an-object-vb-net) or if you don't mind converting then [this](https://stackoverflow.com/questions/2546138/deserializing-json-data-to-c-sharp-using-json-net?rq=1) – A Friend Sep 06 '17 at 08: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! – J86 Sep 06 '17 at 08: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 – A Friend Sep 06 '17 at 08:21
  • hmm, can you show me how please in an answer? – J86 Sep 06 '17 at 08:22

1 Answers1

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.

A Friend
  • 2,750
  • 2
  • 14
  • 23
  • Am I right that I first need to do `Install-Package Newtonsoft.Json -Version 10.0.3`? – J86 Sep 06 '17 at 08:38
  • Whichever compatible version, yes. If you prefer not to install the package then [this link](https://stackoverflow.com/questions/8118019/vb-net-json-deserialize) I provided has a `JavascriptSerializer` method – A Friend Sep 06 '17 at 08:39