how to do json parsing in iphone - iphone

i am using json parsing in my application
and this is my json data is as follow :
{"response" :
{"success":false,"error":
{"code":7,"description":"You are not logged in"}}}
and i want description means "You are not logged in" i my string
so how can i do that
please help me.....

Check the blog post with sample code and step by step parsing of JSON.
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/
http://pessoal.org/blog/2008/12/12/iphone-sdk-parsing-json-data/
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/

We're using CJSONDeserializer (TouchJSON library) in the iPhone app being developed at work.
Just use the following method:
NSDictionary * dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
where data is of type NSData *. It will convert the JSON string into a dictionary so you could get the value of description as follows:
[[[dictionary objectForKey:#"response"] objectForKey:#"error"] objectForKey:#"description"];

Related

Creating Objects from JSON

I would like to be able to create an NSArray or NSDictionary with a JSON string that I receive from a web service. I am using the json framework to do this. The response string will look something along these lines:
{count:2,
data:[{"ID":8,
"Title":"Test Title",
"Author":"Test Author",
"Price":"18.00",
"Edition":"1st",
"Condition":"Good",
"UploadDate":"2012-07-28 07:25:56.0"
},
{"ID":9,
"Title":"Test Title",
"Author":"Test Author",
"Price":"18.000000000000",
"Edition":"1st",
"Condition":"Good",
"UploadDate":"2012-07-28 07:27:06.0"
}
]
}
My question is, what is the most efficient way to grab all of the data from the 'data' array and use it to create either an NSArray or NSDictionary? Any help would be much appreciated
I use RestKit for JSON parsing. It has all you need to fetch the json from a server, translate it into a dictionary, and from there into an object that you can work with. That might be more than you dared to wish for, but it works like a charm. Check out https://github.com/RestKit/RestKit for more info.
Starting from iOS5 a json parser has been integrated as a SDK native component see the following link http://www.raywenderlich.com/5492/working-with-json-in-ios-5.
You will create a NSDictionary with 2 keys "count" and "data" and then you will get the NSArray using objectForKey with the "data" key.

Need some help accessing JSON Data iPhone

Disclaimer: I'm new to JSON.
I have a website that offers up it's Data in JSON format and I'm in the process of building an iPhone App to access that data. The problem I have is not actually with the app itself but rather the syntax to access some of the JSON data.
I don't have any issue accessing the data in the Post [ ] Section, however I want to access Attachments > Images > wptouch-new-thumbnail > url
I know you usually would access the data with a dot syntax but with everything I have tried I have been unable to select anything outside of the Post[] section.
it's pretty simple I take what I get from the server through posts and add them to NSStrings or UIImage in the case of the images for use in a UITableView.
I have all of that working however I can't seem to access anything under attachments. I tried to make a different NSDictionary for attachments but that did not work.
Again remember I'm very new to JSON / Accessing data within it.
Any help would be appreciated!
Make NSMutableData as jsonMutableData instead of NSMutableString.
in connection dedreceiveresponse, initialize jsonMutableData then,
//Json Connection
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[jsonMutableData appendData:partialData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonData = [[NSString alloc] initWithData:jsonMutableData encoding:NSUTF8StringEncoding];
NSDictionary *filesJSON = [jsonData JSONValue];
NSArray *files = [filesJSON objectForKey:#"posts"];
//further code

oData Objective-C add a JSon stream

I try to add a Json strem form my odata server.
It work fine with jQuery, but in objective-c ther is only xml returned.
I try this :
DataModel *proxy = [[DataModel alloc] initWithUri:#"http://www.example.com/odata/odata.svc" credential:nil];
[proxy addHeader:#"Accept" headerValue:#"application/json, text/javascript, */*; sq=0.01"];
QueryOperationResponse *response = [proxy execute:#"Example"];
NSMutableArray *array = [response getResult];
It doesn't work.
How can I do?
thanks
Gwennin
xml is what odata will return. you will need to parse the results. Here is a link to a great tutorial for doing that.
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
If the returned type is a Collection<> of some sort, the OData library does the work for you and converts it to an array of the correct Entity in Objective-C.
If you return a simple type, parse the result. An example is using XPath queries (although for my service, I had to remove from the returned XML a couple of unsupported root element attributes that Odata returned).
If you chose to use Xpath,I love this code: http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html

Retrieving facebook friendlist and showing it in Tableview

I am using Facebook graph api to retrieve facebook's friend list.
I'm using this code:
-(IBAction)getMeFriendsButtonPressed:(id)sender
{
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:#"me/friends" withGetVars:nil];
NSLog(#"getMeFriendsButtonPressed: %#", fb_graph_response.htmlResponse);
}
And in return I am getting this at console.
getMeFriendsButtonPressed:
{"data":[{"name":"name1","id":"504181912"},
{"name":"name2","id":"505621879"},
{"name":"name3","id":"520845156"},
{"name":"name4","id":"537539375"}
}
or something like this.
I want to get names, like name1, name2, name3 and show it in table view.
How can I create an array of these name?
Suggestion please
Thanks
This is JSON output. You may refer to the JSON parsing tutorial at this link
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/
Hope this works for you.
The respone your are getting is JSON. You should use JSON parser for parsing the JSON String
JSON Framework

How to Separate HTML Response

I am using FConnect in my app. After the user logs in to his/her Facebook account I am getting his/her information as:
{
"id":"###########",
"name":"Vishnu Gupta",
"first_name":"Vishnu",
"last_name":"Gupta",
"link":"facebook a/c link id=#######",
"education":[{"school":{"id":"######","name":"st.joseph"},"type":"High School"}],
"gender":"male","email":"##########",
"timezone":5.5,
"locale":"en_US",
"verified":true,
"updated_time":"2010-11-27T10:10:25+0000"
}
I want to store user's information in database. To do that, I have to separate the user's information that is id, name, and email.
I tried using componentsSeparatedByString method but I do not understand how to retrieve the information in an array.
Can anyone help me????
The response you are getting is in JSON Format which basically have response in Array & Dictionary.
You will be getting this data in Dictionary..so you can access it by using the key value..
NSLog(#"%#",[NSDictionaryObject valueforkey:#"id"]);
NSLog(#"%#",[NSDictionaryObject valueforkey:#"name"]);
NSLog(#"%#",[NSDictionaryObject valueforkey:#"first_name"]);
Hope this will surely work for you.....
ya.....its corrrect.In detail:
Add JSON SDK then write the below code
SBJSON *json = [[SBJSON alloc] init];
NSDictionary *returnObject = [json objectWithString:InfoName];
NSString #id=[NSString stringWithFormat:#"%#",[returnObject objectForKey:#"id"]];
same for name ,email.
then Add these objects to Array and release the json
JSOn is good to use for "key-value' pair parsing.