Solved

ASP.NET/VB: Get values from JSON

Posted on 2013-12-23
3
889 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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