Link to home
Start Free TrialLog in
Avatar of shahjagat
shahjagatFlag for United States of America

asked on

Need to Interpret Json in Vb.net ( .net 3.5 framework)

Hello All,

We are into process of migrating from Bing Map v7 to Bing Map v8.
From last many years we are using Visual Studio 2008 and Bing Map Version 7.
We recently have received email from Microsoft that June-30-2017 is declared as end of life for Bing Map v7.

Our integrated/production environment is set up like below
1. We are dependent on SOAP based services of Bing Map V7 ( For geocoding, etc... )
2. We are also dependent on Javascript based services of Bing Map V7 Interaction ( for loading map etc...)
3. we are using .net 3.5 framework


Now Microsoft in V8 is stopping SOAP service completely and They are provide only REST based service

My Problem Definition
 Microsoft had provided great materials for javascript migration but they have not provided good material for migration of Soap to Rest Services.
  While working on rest response of Bing Map V8, I found that their response is in JSON format. then I came across below list of libraries
1. Newton.json library
2. http://www.json.org/

My Question to you all is that
I need your help in finding open source VB.net library ( if any ) so that I should have good control on the entire code for future issues.

Thanks in advance.
Avatar of ste5an
ste5an
Flag of Germany image

Newtonsoft is the standard. Thus use it.

[..] entire code for future issues.
OOP and SOLID are your friends. Create your service façade of the bing services as an interface. Implement your code against it. This will give you the necessary safety cause you've decoupled your code from the external service.
Not to mention that parsing JSON is not so daunting once you get used to the format.  JSON objects represent javascript objects which are *usually* serialized as strings.

The curly braces ({}) identify regular objects.  A regular json objects contains key:value pairs delimited by commas; e.g. -
{ "one": "1", "two": 2 }

Open in new window

The object just defined would be represented like this in VB.NET -
Class MyObject
	Public Property One() As String
	Public Property Two() As Integer
End Class

Open in new window

JSON Objects can also be well-defined; e.g. -
"oneTwo": { "one": "1", "two": 2 }

Open in new window

To parse this definition of the object, we would change the preceeding VB.NET class as such -
Class OneTwo
	Public Property One() As String
	Public Property Two() As Integer
End Class

Open in new window

You could also use attributes to map the object; e.g. -
<JsonObject("OneTwo")> _
Class MyObject
	Public Property One() As String
	Public Property Two() As Intger
End Class

Open in new window

I won't go into great detail about attributes just want to include a teaser.  You can read more about attributes here: http://www.newtonsoft.com/json/help/html/serializationattributes.htm

Carrying on, the brackets in JSON ([]) represent array objects.  Array objects contain elements seperated by commas.  The array elements are nothing more than objects; e.g. -
[{ "one": "1", "two": 2 }, { "one": "3", "two", 4 }, { "one": "5", "two": 6 }]

Open in new window

The most common representation of a JSON array in VB.NET is either an array or a list.

Again, keep in mind that arrays, and their contents, can be well-defined as well; e.g. -
{"oneTwos": [{ "oneTwo": { "one": "1", "two": 2 }}, { "oneTwo": { "one": "3", "two": 4 }}, { "oneTwo": { "one": "5", "two": 6 }}]}

Open in new window


-saige-
Avatar of shahjagat

ASKER

First of all Thanks a lot Saige and Ste5an for your response.

can you please confirm my below doubts
1. is Nuget is available only for .net 4.5 framework, because we are using .net 3.5
2. how to find middle path with which I should not have to migrate to .net 4.5 and I am able use nuget library

Thanks in advance
While doing research I come across " System.Web.Script.Serialization "
I attempted to use it i works well interpret well with simple json string, but it is not able to interpret below string.                                                
 it gives me this error         "Value cannot be null. Parameter name: type"

Below is string from bing map for one of our input and FYI I am VS2008 ( vb.net )

My Code

Dim vBM_String = "{""authenticationResultCode"":""ValidCredentials"",""brandLogoUri"":""http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png"",""copyright"":""Copyright © 2017 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation."",""resourceSets"":[{""estimatedTotal"":1,""resources"":[{""__type"":""Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1"",""bbox"":[28.3878372824293,-97.7645242687346,28.3955627175707,-97.7528157312654],""name"":""602 S Alta Vista Blvd, Beeville, TX 78102"",""point"":{""type"":""Point"",""coordinates"":[28.3917,-97.75867]},""address"":{""addressLine"":""602 S Alta Vista Blvd"",""adminDistrict"":""TX"",""adminDistrict2"":""Bee"",""countryRegion"":""United States"",""formattedAddress"":""602 S Alta Vista Blvd, Beeville, TX 78102"",""locality"":""Beeville"",""postalCode"":""78102""},""confidence"":""High"",""entityType"":""Address"",""geocodePoints"":[{""type"":""Point"",""coordinates"":[28.3917,-97.75867],""calculationMethod"":""Rooftop"",""usageTypes"":[""Display""]},{""type"":""Point"",""coordinates"":[28.3918349436365,-97.7583584300792],""calculationMethod"":""Rooftop"",""usageTypes"":[""Route""]}],""matchCodes"":[""Good""]}]}],""statusCode"":200,""statusDescription"":""OK"",""traceId"":""b302f769b0f9451fa27dde0db0c07aad|HK20280423|7.7.0.0|""}"

   Dim vBM_JSon = New JavaScriptSerializer()
        Dim SSBM_model As SSGeocode = vBM_JSon.Deserialize(Of SSGeocode)(vBM_String)


        HttpContext.Current.Response.Write("<br>")
        HttpContext.Current.Response.Write(SSBM_model.statusCode)
        HttpContext.Current.Response.Write("<br>")
        HttpContext.Current.Response.Write(SSBM_model.authenticationResultCode)

Open in new window











My Class
Public Class SSGeocode

    Private _authenticationResultCode As String
    Private _statusCode As String
    Private _resourceSets As Object


    Public Property authenticationResultCode() As String
        Get
            Return _authenticationResultCode
        End Get
        Set(ByVal value As String)
            _authenticationResultCode = value
        End Set
    End Property

    Public Property statusCode() As String
        Get
            Return _statusCode
        End Get
        Set(ByVal value As String)
            _statusCode = value
        End Set
    End Property

    Public Property resourceSets() As Object
        Get
            Return _resourceSets
        End Get
        Set(ByVal value As Object)
            _resourceSets = value
        End Set
    End Property
End Class

Open in new window

The NuGet Package Manager (NPM) has been available in Visual Studio since 2010.  It is not .NET specific.  It *is*, however, powershell 2.0 depended.  There are projects available on GitHub that even allow for NuGet to be used on VS 2005 and VS 2008; e.g. - https://github.com/paypal/sdk-core-dotnet/wiki/Using-Nuget-in-Visual-Studio-2005-&-2008

Even if you cannot use NuGet, the Newtonsoft Json.NET Library supports .NET 2, .NET 3.5, .NET 4, .NET 4.5, Silverlight, Windows Phone and Windows 8 Store and it can be downloaded and installed manually.

http://www.newtonsoft.com/json/help/html/Introduction.htm

-saige-
just type nuget in the Quick Lunch box in Visual Studio (upper-right corner) after loading your project. NuGet will retrieve and add the correct .NET version of the packages according to your project settings.
SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello -saige-,

Big thanks about your effort.

I copied your code in my project, but I am not get value of
geoCode.ResourceSets(0).Resources(0).Type

As per string given to you value should be " Location:http://schemas.microsoft.com/search/local/ws/rest/v1 ", but I am getting it as blank,  I am not sure but I suspect it because inside json it prefix with 2 under_score special character like  

"__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1"

Open in new window


Please answer

Thanks in advance
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot -saige-

I really appreciate your effort.
Thanks to all