Solved

ASP.NET/VB: Get values from JSON

Posted on 2013-12-23
3
866 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
ID: 39737922
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
ID: 39738070
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
ID: 39742025
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

11 Experts available now in Live!

Get 1:1 Help Now