Solved

ASP.NET/VB: Get values from JSON

Posted on 2013-12-23
3
908 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 46

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

632 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