Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET/VB: Get values from JSON

Posted on 2013-12-23
3
Medium Priority
?
961 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 1800 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 46

Assisted Solution

by:aikimark
aikimark earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

916 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