Solved

how do you parse a json object in vb.net

Posted on 2011-03-23
17
1,039 Views
Last Modified: 2012-05-11
How do you parse this out, all I need is the place name and place id for my app.

This is the data I am playing with.

{"data":[{"name":"Sentinel Pointe Retirement Community","category":"Local business","location":{"street":"2900 Thornhills Avenue SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546","latitude":42.909184900032,"longitude":-85.508770925153},"id":"189676653521"},{"name":"Thornapple Community Church","category":"Local business","location":{"latitude":42.91,"longitude":-85.5077778},"id":"151627728182096"},{"name":"New Equipment Leasing, Inc.","category":"Local business","location":{"street":"2892 Thornhills Ave. SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546","latitude":42.911941,"longitude":-85.507842},"id":"133831559987315"},{"name":"R-Athletics","category":"Sports\/recreation\/activities","location":{"street":"2845 Thornhills Ave","city":"East Grand Rapids","state":"MI","country":"United States","zip":"49546","latitude":42.912379,"longitude":-85.508243},"id":"115170038523942"},{"name":"Pizza Hut","category":"Local business","location":{"street":"6390 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6916","latitude":42.912963,"longitude":-85.510179},"id":"121063367908770"},{"name":"Plowshare Studio","category":"Local business","location":{"latitude":42.912689175,"longitude":-85.51271441},"id":"170773886267034"},{"name":"Culvers","category":"Local business","location":{"street":"6300 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6916","latitude":42.912963,"longitude":-85.512029},"id":"117503338267969"},{"name":"Hair Designs LTD","category":"Local business","location":{"street":"6400 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6918","latitude":42.912963,"longitude":-85.510005},"id":"119505404766777"},{"name":"Starbucks","category":"Local business","location":{"city":"Grand Rapids","state":"MI","country":"United States","zip":"49588","latitude":42.913147598931,"longitude":-85.508967469191},"id":"146959881993784"},{"name":"Endurance Fitness","category":"Local business","location":{"street":"6437 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6917","latitude":42.913237,"longitude":-85.509363},"id":"117793904913583"},{"name":"Cascade Sports Grill","category":"Restaurant\/cafe","location":{"street":"6240 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546","latitude":42.912963,"longitude":-85.513301},"id":"264328352769"},{"name":"Peking Wok's Restaurant","category":"Local business","location":{"street":"6264 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6902","latitude":42.912963,"longitude":-85.512593},"id":"104912652885464"},{"name":"Subway Restaurant","category":"Local business","location":{"street":"6260 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6902","latitude":42.912963,"longitude":-85.512655},"id":"120224134660156"},{"name":"Thai House Restaurant","category":"Local business","location":{"street":"6447 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6917","latitude":42.913237,"longitude":-85.509189},"id":"100871326624715"},{"name":"Black Belt Tae Kwon DO Academy","category":"Local business","location":{"street":"6270 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6902","latitude":42.912963,"longitude":-85.512499},"id":"119985774684484"},{"name":"Akasaka Sushi","category":"Local business","location":{"street":"6252 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6914","latitude":42.912963,"longitude":-85.51278},"id":"118268191522480"},{"name":"Cascade Liquor","category":"Local business","location":{"street":"6262 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6902","latitude":42.912963,"longitude":-85.512624},"id":"122673677781923"},{"name":"Papa John's Pizza","category":"Local business","location":{"street":"6479 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6917","latitude":42.913237,"longitude":-85.508634},"id":"108168982557737"},{"name":"Belladonna Salon & Spa","category":"Local business","location":{"street":"6244 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6914","latitude":42.912963,"longitude":-85.51325},"id":"152225644789674"},{"name":"Cascade Gym","category":"Local business","location":{"street":"6437 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6917","latitude":42.913237,"longitude":-85.509363},"id":"111753072194896"},{"name":"Studio 15","category":"Local business","location":{"street":"6455 28th St. Suite 15","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546","latitude":42.913237,"longitude":-85.509051},"id":"123406044344452"},{"name":"Papa John's Pizza #3171","category":"Local business","location":{"street":"6479 28TH ST SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546","latitude":42.9131,"longitude":-85.5084},"id":"193595667339389"},{"name":"Quizno's Classic Subs","category":"Local business","location":{"street":"6275 28th St SE Ste D","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6970","latitude":42.913237,"longitude":-85.51242},"id":"109643885737569"},{"name":"GNC","category":"Local business","location":{"street":"6467 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6917","latitude":42.913237,"longitude":-85.508842},"id":"151438954869192"},{"name":"Jets Pizza","category":"Local business","location":{"street":"6275 28th St SE","city":"Grand Rapids","state":"MI","country":"United States","zip":"49546-6970","latitude":42.913237,"longitude":-85.51242},"id":"116072495087008"}],"paging":{"next":"https:\/\/graph.facebook.com\/search?access_token=110520359027296\u00257C5676c71d774f0f0f934893cd-100002162196933\u00257ClKwplOd0Tv5gvvzpUR4Y3hbZ9fc&q=_&type=place¢er=42.909244\u00252C-85.510502&distance=1000&limit=25&offset=25"}}
0
Comment
Question by:JDEE8297
  • 8
  • 7
  • 2
17 Comments
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
is this data returned in an HTML page? If so, you will find it easier to parse the results as html than string that you have above.

You can utilize regex to parse the string, though I can not suggest any code.
0
 

Author Comment

by:JDEE8297
Comment Utility
it comes back from a call that I make to facebook using the api for checkins
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
not familiar with the facebook api, but MOST contemporary APIs let you choose the return data type, can you choose XML? (thatwould sure make your task a lot easier).

Like I said, I am unable to give any code suggestion for regex (which I am confident can be achieved). Parsing the string as it is is some task since (at least to VB) the string already contains quotes within.It can be done, but messy ...... and if you are looking to automate the process, would be not only tedious but prone to errors thus I will not attempt that for now.
0
 

Author Comment

by:JDEE8297
Comment Utility
I wish it was xml, it would be alot easier to work with, but it is a json object and really not finding good examples out there....vb or c#. If it was in c# I could convert it to vb.net code, but alas that is not the case on this one. Oh well...thanks for you help.
0
 

Author Comment

by:JDEE8297
Comment Utility
okay, I made some headway on this, but I am still not seeing any results. So if anyone has any idea on this, let me know.

Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json

<DataContract()> _
Public Class FBCheckIns
    Private _strName As String = String.Empty
    Private _strCategory As String = String.Empty
    Private _strId As String = String.Empty
    Public Sub New()
    End Sub
    Public Sub New(ByVal name As String, ByVal category As String, ByVal id As String)
        Me.name = name
        Me.Category = category
        Me.Id = id
    End Sub
    <DataMember(name:="name")>
    Public Property name As String
        Get
            Return _strName
        End Get
        Set(ByVal value As String)
            _strName = value
        End Set
    End Property
    <DataMember(name:="category")>
    Public Property Category As String
        Get
            Return _strCategory
        End Get
        Set(ByVal value As String)
            _strCategory = value
        End Set
    End Property
    <DataMember(name:="id")>
    Public Property Id As String
        Get
            Return _strId
        End Get
        Set(ByVal value As String)
            _strId = value
        End Set
    End Property

End Class

 Dim Checkins As New FBCheckIns()
            Dim ms As New MemoryStream(Encoding.Unicode.GetBytes(j2))
            Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(Checkins.GetType())
            Checkins = TryCast(serializer.ReadObject(ms), FBCheckIns)

      ms.Close()

            Response.Write("Name = " & Checkins.Id)

Open in new window


It could be that I really do need to have another object in there to handle location piece of the data, or this is right, but I still can't see any values in my object....why :(
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
How/Where are you loading or passing or getting this JSON object ?
From client side ? Could you share this part ?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>it comes back from a call that I make to facebook using the api for checkins

jsonp ? The call is done on the client side (browser) ?

If it's the case you can parse it easily with javascript to get what you want and send only part of it

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Check the following, we return only a string, the next value of the paging object :

https:\/\/graph.facebook.com\/search?access_token=110520359027296\u00257C5676c71d774f0f0f934893cd-100002162196933\u00257ClKwplOd0Tv5gvvzpUR4Y3hbZ9fc&q=_&type=place¢er=42.909244\u00252C-85.510502&distance=1000&limit=25&offset=25

A good link to parse the object : http://jsonviewer.stack.hu/
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.IO
Imports System.Text
Imports System.Xml.Serialization
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json
Imports System.Collections.Generic

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    <Script.Services.ScriptMethod(ResponseFormat:=Script.Services.ResponseFormat.Json)> _
    Public Function getPaging(ByVal j2 As String) As String
        Dim Checkins As New FBCheckIns()
        Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(j2))
        Dim serializer As New DataContractJsonSerializer(Checkins.GetType())
        Checkins = TryCast(serializer.ReadObject(ms), FBCheckIns)
        ms.Close()
        Return Checkins.Paging.Nxt
    End Function

    <DataContract()> _
    Public Class FBCheckIns

        Private _data As List(Of Data)
        Private _paging As Paging

        Public Sub New()
        End Sub

        Public Sub New(ByVal data As List(Of Data), ByVal paging As Paging)
            Me._data = data
            Me._paging = paging
        End Sub

        <DataMember(Name:="data")> _
        Public Property Data() As List(Of Data)
            Get
                Return _data
            End Get
            Set(ByVal value As List(Of Data))
                _data = value
            End Set
        End Property

        <DataMember(Name:="paging")> _
        Public Property Paging() As Paging
            Get
                Return _paging
            End Get
            Set(ByVal value As Paging)
                _paging = value
            End Set
        End Property

    End Class

    <DataContract()> _
    Public Class Data

        Private _name As String
        Private _category As String
        Private _location As Location
        Private _id As String

        Public Sub New()
        End Sub

        Public Sub New(ByVal name As String, ByVal category As String, ByVal location As Location, ByVal id As String)
            Me._name = name
            Me._category = category
            Me._location = location
            Me._id = id
        End Sub

        <DataMember(Name:="name")> _
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        <DataMember(Name:="category")> _
        Public Property Category() As String
            Get
                Return _category
            End Get
            Set(ByVal value As String)
                _category = value
            End Set
        End Property

        <DataMember(Name:="location")> _
        Public Property Location() As Location
            Get
                Return _location
            End Get
            Set(ByVal value As Location)
                _location = value
            End Set
        End Property

        <DataMember(Name:="id")> _
        Public Property Id() As String
            Get
                Return _id
            End Get
            Set(ByVal value As String)
                _id = value
            End Set
        End Property

    End Class

    <DataContract()> _
    Public Class Paging

        Private _next As String

        Public Sub New()
        End Sub

        Public Sub New(ByVal nxt As String)
            Me._next = nxt
        End Sub

        <DataMember(Name:="next")> _
        Public Property Nxt() As String
            Get
                Return _next
            End Get
            Set(ByVal value As String)
                _next = value
            End Set
        End Property

    End Class

    <DataContract()> _
    Public Class Location

        Private _street As String
        Private _city As String
        Private _state As String
        Private _country As String
        Private _zip As String
        Private _latitude As Decimal
        Private _longitude As Decimal

        Public Sub New()
        End Sub

        Public Sub New(ByVal street As String, ByVal city As String, ByVal state As String, ByVal country As String, ByVal zip As String, ByVal latitude As String, ByVal longitude As String)
            Me._street = street
            Me._city = city
            Me._state = state
            Me._country = country
            Me._zip = zip
            Me._latitude = latitude
            Me._longitude = longitude
        End Sub

        <DataMember(Name:="street")> _
        Public Property Street() As String
            Get
                Return _street
            End Get
            Set(ByVal value As String)
                _street = value
            End Set
        End Property

        <DataMember(Name:="city")> _
        Public Property City() As String
            Get
                Return _city
            End Get
            Set(ByVal value As String)
                _city = value
            End Set
        End Property

        <DataMember(Name:="state")> _
        Public Property State() As String
            Get
                Return _state
            End Get
            Set(ByVal value As String)
                _state = value
            End Set
        End Property

        <DataMember(Name:="country")> _
        Public Property Country() As String
            Get
                Return _country
            End Get
            Set(ByVal value As String)
                _country = value
            End Set
        End Property

        <DataMember(Name:="zip")> _
        Public Property Zip() As String
            Get
                Return _zip
            End Get
            Set(ByVal value As String)
                _zip = value
            End Set
        End Property

        <DataMember(Name:="latitude")> _
        Public Property Latitude() As Decimal
            Get
                Return _latitude
            End Get
            Set(ByVal value As Decimal)
                _latitude = value
            End Set
        End Property

        <DataMember(Name:="longitude")> _
        Public Property Longitude() As Decimal
            Get
                Return _longitude
            End Get
            Set(ByVal value As Decimal)
                _longitude = value
            End Set
        End Property


    End Class

End Class

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:JDEE8297
Comment Utility
All of this is taking place on the server side, I make the call to the facebook checkins api, which in turn gives me a list of places in a json object. I plan to parse the object out and pull out the following information:

1. Name
2. Id

And from there populate a dropdown list, that a end user will select and then post a messge, and I send that onto facebook. Sounds pretty straightforward on paper, so any help ye can give is appreciated as always. :)
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
using a loop :

Checkins.Data.ElementAt(i).Name return the name

Checkins.Data.ElementAt(i).Id return the name

For i = 0 To Checkins.Data.Count - 1
            Dim name = Checkins.Data.ElementAt(i).Name
            Dim id = Checkins.Data.ElementAt(i).Id
            ' Do something with name and id
        Next i

Open in new window

0
 

Author Comment

by:JDEE8297
Comment Utility
@leakim971 I don't have to use your code as a webservice, I should be able to incorporate that iinto what I have on my page.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
so use the classes
0
 

Author Comment

by:JDEE8297
Comment Utility
that was what I was double checking on, I will give it a go and see comes of it.
0
 

Author Closing Comment

by:JDEE8297
Comment Utility
thank you thank you thank you.....I know it may sound silly to say it like that, but that worked like a charm and now I am very close to getting this done. Some cleaning up to do, but I got it to work with your code.

I ended up not using it ias a web service, but used the classes. So thanks again for your help.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
ok, as web service it work fine with your JSON object
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Oh great! Did not see your last comment before posting my last one! Thanks for the points!
0
 

Author Comment

by:JDEE8297
Comment Utility
you are very welcome. :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 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

13 Experts available now in Live!

Get 1:1 Help Now