Solved

ASP.NET/VB: Get values from JSON

Posted on 2013-12-23
3
856 Views
Last Modified: 2013-12-27
Using ASP.NET/VB, how can I get values from JSON?  In this example, I want "abc" to be returned.
<%@ Page Language="VB"%>
<%

Dim data As String = "{""total_rows"":1,""offset"":0,""rows"":[ {""id"":""abc"",""key"":""abc"",""value"":{""rev"":""1-967a00dff5e02add41819138abb3284d""}} ]}"

HttpContext.Current.Response.Write(data.rows.id)

%>

Open in new window

0
Comment
Question by:hankknight
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 450 total points
Comment Utility
2 ways I can think of (I believe there are more, depending on the framework version you're using, I'm still on 4)

1) download Newtonsoft.Json dll with nuget (http://nuget.org/packages/Newtonsoft.Json ) or here: http://json.codeplex.com/ and add a reference to your project.

my actual markup is different but should be something like this:
<%@ Page Language="VB"%>
<%@ Import Namespace="Newtonsoft.Json.Linq" %>
<%

Dim data As String = "{""total_rows"":1,""offset"":0,""rows"":[ {""id"":""abc"",""key"":""abc"",""value"":{""rev"":""1-967a00dff5e02add41819138abb3284d""}} ]}"

Dim objJSON As JObject = JObject.Parse(data)
Dim firstRow As JToken = objJSON.SelectToken("rows").First()

HttpContext.Current.Response.Write(firstRow("id").ToString())

%>

Open in new window


2) define classes for your objects and use JavaScriptSerialiser (here I did it in code-behind file Alternative.aspx.vb):
Imports System.Web.Script.Serialization

Public Class Alternative
    Inherits System.Web.UI.Page

    Dim obj As ResultRows

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim data As String = "{""total_rows"":1,""offset"":0,""rows"":[ {""id"":""abc"",""key"":""abc"",""value"":{""rev"":""1-967a00dff5e02add41819138abb3284d""}} ]}"

        Dim jsd As New JavaScriptSerializer
        obj = jsd.Deserialize(Of ResultRows)(data)

        Response.Write("alternative: " & obj.rows(0).id)

    End Sub

End Class

Public Class ResultRows
    Public total_rows As Integer
    Public offset As Integer
    Public rows() As ResultRow
End Class

Public Class ResultRow
    Public id As String
    Public key As String
    Public value As ResultRevObject
End Class

Public Class ResultRevObject
    Public rev As String
End Class

Open in new window

0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 50 total points
Comment Utility
If you have a lot of JSON data, you should consider the fastJSON library.  Its performance figures against the competitors are impressive.
http://fastjson.codeplex.com/
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now