(Warning: totally inadequate definition follows!) An object is a data item. It can contain data items. It can contain methods to do things, often with data. Some of the .NET languages are not as strict about objects as other languages. The contained data items might be properties (this is where .NET gets a bit lax about things).
Objects are defined using classes.
This is an examples of a class used in the API:
Code:
[CODE]Public Class ClassCountryCodeResult
Public countryCode As String ' ISO-2 code for the country
Public marketCount As Integer ' No of related markets
End Class
As you can see, it has two data items - countryCode and marketCount. The .NET languages allow you to directly read the countryCode and marketCount when they are defined as Public. (I say that; VB certainly does. C# might be more strict).
If I do a listCountryCode API call, I'll get lots of CountryCodes back, so the object for receiving all this data might look like this:
Code:
Public Class ClassListCountryResult
Public jsonrpc As String
Public result() As ClassCountryCodeResult
End Class
When I do a listCountryCode, I'll get a string back. It looks like this, but there's much more; I've put line breaks after the commas (etc) to aid readability:
{"jsonrpc":"2.0",
"result":[
{"countryCode":"GB","marketCount":2874},
{"countryCode":"PL","marketCount":138},
{"countryCode":"UA","marketCount":251},
{"countryCode":"CH","marketCount":821},
{"countryCode":"IE","marketCount":28},
{"countryCode":"SE","marketCount":370}
],
"id":1}
This is a relatively simple example. I have a value for "jsonrpc" and half a dozen ClassCountryCodeResult objects in an array.
Now then, the way NewtonSoftJson works is that in a SINGLE statement it can take all the data from the string and put it into a object defined as ClassListCountryResult.
Rather like this:
If I call the string above inString, then
Code:
Dim countries = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ClassListCountryResult)(inString)
countries.result(0).countryCode
countries.result(0).marketCount
and even if you want to
countries.jsonrpc
though I don't know why you'd want to.
Etc.
You might wonder what happened to the "id" parameter from the JSON string. There was nowhere for it to go, so it got ignored. Easy.
You might need to be more specific and say
Dim countries as ClassListCountryResult (etc)
but VB.NET does a good job assuming you want it that way via the class mentioned in the DeserializeObject statement.
Going from an object to a string is serializing and looks like this:
Code:
Dim aString = Newtonsoft.Json.JsonConvert.SerializeObject(anObject)
So, basically you can convert between strings and objects without writing masses of code to do it.
If you can't find out how to get the NewtonSoft stuff in Excel, you could investigate the JavaScriptSerializer, which does much the same (but with slightly different code). If neither work out, let me know.



Leave a comment: