IT tutorials
 
Mobile
 

iphone Programming : Handling Data - Parsing JSON (part 1) - The Twitter Search Service

1/25/2013 11:10:05 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

JSON is a lightweight data-interchange format, which is more or less human-readable but still easily machine-parsable. While XML is document-oriented, JSON is data-oriented. If you need to transmit a highly structured piece of data, you should probably render it in XML. However, if your data exchange needs are somewhat less demanding, JSON might be a good option.

The obvious advantage JSON has over XML is that since it is data-oriented and (almost) parsable as a hash map, there is no requirement for heavyweight parsing libraries. Additionally, JSON documents are much smaller than the equivalent XML documents. In bandwidth-limited situations, such as you might find on the iPhone, this can be important. JSON documents normally consume around half of the bandwidth as an equivalent XML document for transferring the same data.

While there is no native support for JSON in the Cocoa Touch framework, Stig Brautaset’s json-framework library implements both a JSON parser and a generator and can be integrated into your project fairly simply.

Consuming Ruby on Rails

If you are dealing exclusively with Rails-based services, the ObjectiveResource framework (see http://iphoneonrails.com/ for more details) is a port of the ActiveResource framework of Ruby on Rails. It provides a way to serialize Rails objects to and from Rails’ standard RESTful web services via either XML or JSON. ObjectiveResource adds methods to NSObject using the category extension mechanism, so any Objective-C class can be treated as a remote resource.


Download the disk image with the latest version of the json-framework library from http://code.google.com/p/json-framework/. Open the disk image and drag and drop the JSON folder into the Classes group in the Groups & Files pane of your project. Remember to tick the “Copy items into destination group’s folder” checkbox before adding the files. This will add the JSON source files to your project; you will still need to import the JSON.h file into your class to use it.

Linking to the JSON Framework

Since dynamic linking to third-party embedded frameworks is not allowed on the iPhone platform, copying the JSON source files into your project is probably the simplest way to make the parser available to your application. However, there is a slightly more elegant approach if you don’t want to add the entire JSON source tree to every project where you use it.

Open the Finder and create an SDKs subfolder inside your home directory’s Library folder, and copy the JSON folder located inside the SDKs folder in the disk image into the newly created ~/Library/SDKs directory.

Back in Xcode, open your project and double-click on the project icon at the top of the Groups & Files pane to open the Project Info window. In the Architectures section in the Build tab, double-click on the Additional SDKs field and add $HOME/Library/SDKs/JSON/${PLATFORM_NAME}.sdk to the list of additional SDKs in the pop-up window. Now go to the Linking subsection of the Build tab, double-click on the Other Linker Flags field, and add -ObjC -all_load -ljson to the flags using the pop-up window.

Now you just have to add the following inside your source file:

#import <JSON/JSON.h>

Note the use of angle brackets rather than double quotes around the imported header file, denoting that it is located in the standard include path rather than in your project.


1. The Twitter Search Service

To let you get familiar with the json-framework library, let’s implement a bare-bones application to retrieve the trending topics on Twitter by making use of their RESTful Search API.


Note:

If you’re interested in the Twitter API, you should definitely look at Twitter’s documentation for more details regarding the available methods. However, if you’re serious about using the Twitter API, you should probably look into using the MGTwitterEngine library written by Matt Gemmell. You can download it from http://mattgemmell.com/source.


Making a request to the Twitter Search API of the form http://search.twitter.com/trends.json will return a JSON document containing the top 10 topics that are currently trending on Twitter. The response includes the time of the request, the name of each trend, and the URL to the Twitter Search results page for that topic:

{
     "trends":[
        {
           "name":"#musicmonday",
           "url":"http:\/\/search.twitter.com\/search?q=%23musicmonday"
        },
        {
           "name":"Spotify",
           "url":"http:\/\/search.twitter.com\/search?q=Spotify+OR+%23Spotify"
        },
        {
           "name":"Happy Labor Day",
           "url":"http:\/\/search.twitter.com\/search?q=%22Happy+Labor+Day%22"
        },
        {
           "name":"District 9",
           "url":"http:\/\/search.twitter.com\/search?q=%22District+9%22"
        },
        {
           "name":"Goodnight",
           "url":"http:\/\/search.twitter.com\/search?q=Goodnight"
        },
        {
           "name":"Chris Evans",
           "url":"http:\/\/search.twitter.com\/search?q=%22Chris+Evans%22"
        },
        {
           "name":"iPhone",
           "url":"http:\/\/search.twitter.com\/search?q=iPhone+OR+%23Iphone"
        },
        {
           "name":"Jay-Z",
           "url":"http:\/\/search.twitter.com\/search?q=Jay-Z"
        },
        {
           "name":"Dual-Screen E-Reader",
           "url":"http:\/\/search.twitter.com\/search?q=%22E-Reader%22"
        },
        {
           "name":"Cadbury",
           "url":"http:\/\/search.twitter.com\/search?q=Cadbury"
        }
     ],
     "as_of":"Mon, 07 Sep 2009 09:18:34 +0000"
}

				
 
Others
 
- Android : Using Selection Widgets - . Fields
- Android : Using Selection Widgets - Grid Your Lions (or Something Like That...)
- Windows Phone 8 : XAML Overview - Transformations and Animations
- Windows Phone 8 : XAML Overview - Images
- Mobile Web Development with WordPress, Joomla!, and Drupal : PRIMARY SITE CONTENT (part 3) - Forms
- Mobile Web Development with WordPress, Joomla!, and Drupal : PRIMARY SITE CONTENT (part 2) - Embedding Images and Media
- Mobile Web Development with WordPress, Joomla!, and Drupal : PRIMARY SITE CONTENT (part 1) - Text and Typography, Pagination
- BlackBerry Bold 9700 and 9650 Series : Email Set Up - Setting Up Email for the First Time
- BlackBerry Bold 9700 and 9650 Series : Email Set Up - Learn Your BlackBerry and Getting Around
- Enter Java ME on Symbian OS : Java ME Management on Devices (part 2)
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us