Sudhanshum
asked on
Parsing Json string
Hi, I have json string and I want to parse in vb.Net so I can select individual values, What will be simplest way to do that, I am attaching my code
Dim serviceUrl As String = "http://localhost:61210/Webservice/PService.svc"
Dim input As Object = New With { _
.UserId = "14"
}
Dim inputJson As String = (New JavaScriptSerializer()).Serialize(input)
Dim httpRequest As HttpWebRequest = DirectCast(WebRequest.Create(New Uri(serviceUrl & "/InsertLogInLog")), HttpWebRequest)
httpRequest.Accept = "application/json"
httpRequest.ContentType = "application/json"
httpRequest.Method = "POST"
Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputJson)
Using stream As IO.Stream = httpRequest.GetRequestStream()
stream.Write(bytes, 0, bytes.Length)
stream.Close()
End Using
Dim strText As String
Using httpResponse As HttpWebResponse = DirectCast(httpRequest.GetResponse(), HttpWebResponse)
Using stream As IO.Stream = httpResponse.GetResponseStream()
strText = (New StreamReader(stream)).ReadToEnd()
End Using
End Using
ASKER
Thanks for reply, I am using Windows server 2010 and .Net 4.0, How I can add this library there?
You are using Visual Basic.NET correct? Have a look at the Microsoft documentation: Quickstart: Install and use a package in Visual Studio (Windows only)
ASKER
Yes but I am using Visual Studio 2010 but it need Visual Studio 2019
Have a look at the following link to add the NuGet package to Visual Studio 2010
Installing NuGet on VS2010
By the way, you can get Visual Studio 2019 for free.
Installing NuGet on VS2010
By the way, you can get Visual Studio 2019 for free.
ASKER
Its not just 2019 VS, my Client has server 2008 where it cant be installed,
You wouldn't develop on a server. I would suggest installing Visual Studio 2019 on a compatible OS and then making the changes on that machine before deploying your application to a server.
Hi Sudhanshum,
Once you navigate to: https://www.nuget.org/pack ages/Newto nsoft.Json /, scroll down a bit. You will see Version History, click on Show More and you will see a list of packages available.
Now, you will have to put in some efforts. Download a package that is around August 2012 and you will find a compatible package. Now rename this package to .zip and then open it. You will see multiple folders, get the required assemblies from that folder and you are good to go. Please note that such an old version of the assemblies might carry risks w.r.t. security, stability and compatibility.
Also, you should let your client know that in approximately 6 months (14th January 2020) the mainstream support will end for server 2008. Better upgrade the server.
Also, for any commercial engagement Visual Studio is not free at all.
Regards,
Chinmay.
Once you navigate to: https://www.nuget.org/pack
Now, you will have to put in some efforts. Download a package that is around August 2012 and you will find a compatible package. Now rename this package to .zip and then open it. You will see multiple folders, get the required assemblies from that folder and you are good to go. Please note that such an old version of the assemblies might carry risks w.r.t. security, stability and compatibility.
Also, you should let your client know that in approximately 6 months (14th January 2020) the mainstream support will end for server 2008. Better upgrade the server.
Also, for any commercial engagement Visual Studio is not free at all.
Regards,
Chinmay.
ASKER
Hi Dirk Strauss, I tried user method but getting below error:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Account' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
Here is my code
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Account' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
Here is my code
Imports System.Net
Imports System.Text
Imports System.Web.Script.Serialization
Imports System.IO
Imports Newtonsoft.Json
Public Class Account
Public Property ID As String
Public Property MESSAGE As String
End Class
Partial Class TestWCF
Inherits System.Web.UI.Page
Protected Sub btnClick_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClick.Click
Dim serviceUrl As String = "http://localhost:61210/PWebservice/PService.svc"
Dim input As Object = New With { _
.UserId = "17"
}
Dim inputJson As String = (New JavaScriptSerializer()).Serialize(input)
Dim httpRequest As HttpWebRequest = DirectCast(WebRequest.Create(New Uri(serviceUrl & "/InsertLogInLog")), HttpWebRequest)
httpRequest.Accept = "application/json"
httpRequest.ContentType = "application/json"
httpRequest.Method = "POST"
Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputJson)
Using stream As IO.Stream = httpRequest.GetRequestStream()
stream.Write(bytes, 0, bytes.Length)
stream.Close()
End Using
Dim strText As String
Using httpResponse As HttpWebResponse = DirectCast(httpRequest.GetResponse(), HttpWebResponse)
Using stream As IO.Stream = httpResponse.GetResponseStream()
strText = (New StreamReader(stream)).ReadToEnd()
End Using
End Using
Dim account As Account = JsonConvert.DeserializeObject(Of Account)(strText)
Dim val As String = account.MESSAGE
End Sub
End Class
ASKER
Hi Chinmay Patel,
Your method helped me, I was able to download dll and add that on my project, However My code not working yet
Your method helped me, I was able to download dll and add that on my project, However My code not working yet
Hi Sudhanshum,
Please be more specific. Share your updated code here. Are you getting any errors? If yes, please post the exception details.
Regards,
Chinmay.
Please be more specific. Share your updated code here. Are you getting any errors? If yes, please post the exception details.
Regards,
Chinmay.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
You can have a look at the Json.NET Documentation for code samples. For example, you can do the following:
Open in new window
Now you can deserialize a JSON string to the Account object as follows:Open in new window
This example is from their documentation.