JSON Examples - KingDeath360 - Feb 1, 2015
Just some little JSON examples to help people out.
If you dont know what json looks like, then look below:
Code {""version"":0.3, ""status"":0, ""route_summary"": { ""total_distance"":300, ""total_time"":14, ""start_point"":""43"", ""end_point"":""42"" }, ""route_geometry"":""[[51.025421,18.647631],[51.026131,18.6471],[51.027802,18.645639]]"", ""route_instructions"": [[""Head northwest on 43"",88,0,4,""88 m"",""NW"",334.8],[""Continue on 42"",212,1,10,""0.2 km"",""NW"",331.1,""C"",356.3]]}
Example 1
If your building your application professionally/slowly this will be for you.
Code Imports System.IO
Imports System.Runtime.Serialization.Json
Imports System.Runtime.Serialization
<DataContract(Namespace:="")> _
Public Class Route
<DataMember(Name:="version")>
Public Property version As Double
<DataMember(Name:="status")>
Public Property status As Double
<DataMember(Name:="route_summary")>
Public Property route_summary As route_summary
<DataMember(Name:="route_geometry")>
Public Property route_geometry As Object()
<DataMember(Name:="route_instructions")>
Public Property route_instructions() As Object
End Class
<DataContract(Name:="route_summary", Namespace:="")> _
Public Class route_summary
<DataMember(Name:="total_distance")>
Public Property total_distance As Double
<DataMember(Name:="total_time")>
Public Property total_time As Double
<DataMember(Name:="start_point")>
Public Property start_point As Double
<DataMember(Name:="end_point")>
Public Property end_point As Double
End Class
Public Class Converter(Of t)
Public Function ReturnJSON(ByRef sreader As StreamReader) As t
If GetType(t).Equals(GetType(String)) Then
Dim result As Object = sreader.ReadToEnd.Replace("""", "")
Return result
Else
Dim ds As New DataContractJsonSerializer(GetType(t))
Dim result As t = DirectCast(ds.ReadObject(sreader.BaseStream), t)
ds = Nothing
Return result
End If
End Function
End Class
Sub Main()
Dim json As String = "{""version"":0.3, ""status"":0, ""route_summary"": { ""total_distance"":300, ""total_time"":14, ""start_point"":""43"", ""end_point"":""42"" }, ""route_geometry"":[[51.025421,18.647631],[51.026131,18.6471],[51.027802,18.645639]], ""route_instructions"": [[""Head northwest on 43"",88,0,4,""88 m"",""NW"",334.8],[""Continue on 42"",212,1,10,""0.2 km"",""NW"",331.1,""C"",356.3]]}"
Dim encoding As New System.Text.UTF8Encoding
Dim bytes() As Byte = encoding.GetBytes(json)
Using os As New MemoryStream
os.Write(bytes, 0, bytes.Length)
os.Position = 0
Using reader As New StreamReader(os)
Dim converter As New Converter(Of Route)
Dim output As Route
output = converter.ReturnJSON(reader)
'output contains data
End Using
End Using
End Sub
Example 2
MEDIUM METHOD
Possible useful for creating your own socket protocol where you would convert objects to and from json
Code Dim p As New person
p.name = "testname"
p.age = 17
Dim stream1 As New MemoryStream()
Dim ser As New DataContractJsonSerializer(GetType(person))
ser.WriteObject(stream1, p)
stream1.Position = 0
Dim sr As New StreamReader(stream1)
Console.Write(String.Format("JSON form of Person object:## {0} ##", sr.ReadToEnd()))
Console.Read()
<DataContract(Namespace:="person")> _
Class person
<DataMember(Name:="name")> Public name As String
<DataMember(Name:="age")> Public age As Integer
End Class
Example 3
QUICK METHOD - i find this useful for decoding proxy websites json format
Code Imports System.Net
Imports System.Web.Script.Serialization
Public Class Form1
'System.Web.Extensions
Dim dfsfsdfdsf As String = "{""apiVersion"":""2.1"",""data"":{""updated"":""2013-05-24T12:13:18.281Z"",""totalItems"":1,""startIndex"":1,""itemsPerPage"":25,""items"":[{""id"":""yn ed"":""2013-05-23T18:43:08.000Z"",""updated"":""2013-05- ]}}"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using wb As New WebClient()
Dim json As String = wb.DownloadString(String.Format("http://gdata.youtube.com/feeds/api/videos?q={0}&v=2&alt=jsonc", "ynbJweTvzag"))
Dim serializer As New JavaScriptSerializer()
Dim deserializedData As Dictionary(Of String, Object) = DirectCast(serializer.DeserializeObject(json), Dictionary(Of String, Object))
Dim data As Dictionary(Of String, Object) = TryCast(deserializedData("data"), Dictionary(Of String, Object))
Dim videoInfo As Dictionary(Of String, Object) = TryCast(DirectCast(data("items"), Object())(0), Dictionary(Of String, Object))
Dim uploaded As String = videoInfo("uploaded").ToString()
Dim updated As String = videoInfo("updated").ToString()
Dim uploader As String = videoInfo("uploader").ToString()
Dim category As String = videoInfo("category").ToString()
Dim title As String = videoInfo("title").ToString()
Dim description As String = videoInfo("description").ToString()
Dim duration As String = videoInfo("duration").ToString()
Dim rating As String = videoInfo("rating").ToString()
Dim likeCount As String = videoInfo("likeCount").ToString()
Dim ratingCount As String = videoInfo("ratingCount").ToString()
Dim viewCount As String = videoInfo("viewCount").ToString()
Dim favoriteCount As String = videoInfo("favoriteCount").ToString()
Dim commentCount As String = videoInfo("commentCount").ToString()
MsgBox(uploader)
End Using
End Sub
End Class
NOT MINE
Credits
Microsoft
Daniel Knoodle
http://stackoverflow.com/questions/28147705/working-with-nested-json-file-visual-basic
* - Minimal. - Feb 1, 2015
(Feb 1, 2015 08:43 AM)KingDeath360 Wrote: NOT MINE
How about giving credits to the original author then?
* - KingDeath360 - Feb 1, 2015
(Feb 1, 2015 08:44 AM)Felix. Wrote: How about giving credits to the original author then?
houston we have a problem,
most of these text files with code came from a [MEGA PACK] with no credits:
* - Odus - Feb 1, 2015
(Feb 1, 2015 08:50 AM)KingDeath360 Wrote: houston we have a problem,
most of these text files with code came from a [MEGA PACK] with no credits:
There's also the issue of missing explanations, examples of usage, which snippet is optimal for certain situations, etc.
I've been able to source the first & 2nd rip which both give far more information than OP:
1) http://stackoverflow.com/questions/4955792/deserialize-complex-json-vb-net
2) http://stackoverflow.com/questions/28147705/working-with-nested-json-file-visual-basic / http://homepage1.nifty.com/rucio/main/VBdotNet/Samples201/Sample234DataContractJsonSerializer.htm (???)
Couldn't find No.3, likely from a solution. OP please try harder when making this type of thread.
|