New topic: 

JSON parse help!

<http://forums.realsoftware.com/viewtopic.php?t=45892>

         Page 1 of 1
   [ 2 posts ]                 Previous topic | Next topic          Author  
Message        Akiland          Post subject: JSON parse help!Posted: Tue Nov 
13, 2012 1:21 pm                                 
Joined: Tue Jan 04, 2011 3:02 am
Posts: 1001
Location: Jönköping, Sweden                Hi,
Need some help with JSON today. I've never touched it before so I really have 
no clue how to solve this one.
I'm getting a JSON response from an HTTP API. It contains search results from a 
given string, this part is no problem.
My problem is that I need to get my head around how to parse it in RB.

The JSON respons looks like this:
{
"title": "Gula sidorna API",
"query": 
"http://api.eniro.com/cs/search/basic?to_list=25&from_list=1&search_word=hotell&geo_area=&country={country}&version=1.1.2";,
"totalHits": 4170,
"totalCount": 4170,
"startIndex": 0,
"itemsPerPage": 25,
"adverts": [
{
"eniroId": "14380005",
"companyInfo": {
"companyName": "Clarion Hotel Sign",
"orgNumber": "5564660107",
"companyText": "Stockholms största hotell med 558 dubbelrum, konferenslokaler 
för upp till 700 personer, spa,..."
},
"address": {
"streetName": "Ö. Järnvägsg. 35",
"postCode": "101 26",
"postArea": "STOCKHOLM",
"postBox": "Box 310"
},
"location": {
"coordinates": [
{
"longitude": 18.054142487983395,
"latitude": 59.33432236299209
},
{
"use": "route",
"longitude": 18.054142487983395,
"latitude": 59.33432236299209
}
]
},
"phoneNumbers": [
{
"type": "std",
"phoneNumber": "08 - 676 98 00",
"label": null
}
],
"companyReviews": "http://www.rejta.se/omdome/629185";,
"homepage": 
"http://api.eniro.com/proxy/homepage/uANwPf5aVK02ZfzqpUMFhr0Wb3aYXzIuCPuzuUsc0svRGwBEfv4HOg==";,
"infoPageLink": 
"http://gulasidorna.eniro.se/f/radisson-blu-scandinavia-hotel:3513360?search_word=hotell";
},

..

]
}

This is just an example response and it only contains ONE search result. The 
actual response contains up to 25 results in "adverts".

As you can see the search results is found in "adverts".
I need help on how to Populate a PopupMenu with all the "companyName"s that is 
found.
I've tried to study the JSON example shipped with RB but I might be stupid or 
something as I just don't get how it works 

Anyone got a push in the right direction?     
_________________
 http://www.linkedin.com/in/albinkiland
Dev. iMac 27" + 2x22" LG (2.8GHz Intel Core i7, 12GB RAM, 120GB SSD) OS X 10.8
REAL.Studio Web Edition 2011r3  
                             Top                charonn0          Post subject: 
Re: JSON parse help!Posted: Tue Nov 13, 2012 2:26 pm                            
     
Joined: Mon Apr 02, 2007 2:08 am
Posts: 990
Location: San Francisco, CA, USA                Creating a JSONItem from a 
string is easy enough:

Dim somejson As String 'Pretend this has raw json data
Dim MyJSItem As New JSONItem(somejson) 'You will get an exception here if 
somejson is not valid JSON


A JSONItem in many ways is like a more-flexible Dictionary. JSONItems can 
contain arrays, other JSONItems, and any other datatype including Nil. If a 
JSONItem represents an array then you must treat it like an array or you will 
get an exception (same is true if it's not an array and you treat it like an 
array.)

I find it very, very useful to look at the JSON I'm dealing with in Notepad++ 
with the JSON Viewer plugin (or another similar JSON viewer) since it makes the 
structure much more clear, e.g.:
      
_________________
Boredom Software  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 2 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to