2

I am working on an iPhone app which involves using json-framework.I am getting array using NSURL

[{"firstName":"X","lastName":"Y","id":1},{"firstName":"A","lastName":"B","id":2}]

How can i get these 2 objects as like if i query for id=1, the O/P is

id=1
firstName:X
lastName:Y

and putting it in a table. I am googling the stuff from many days but didn't get any solution. Please help me out , explanation through code is appreciated.

Thank You.

1

2 Answers 2

5

If your target SDK is ios4 or higher, you can use this project

https://github.com/stig/json-framework/

Once you add the source to your project, just

#import "SBJson.h"

and convert your Json string as follows

jsonResponse = [string JSONValue];

The method will fail if you don't have the full Json array in your string, but you can keep appending strings until it doesn't fail

To follow up for codejunkie's request below you can assume in your data structure that the jsonResponse is an NSArray In other implementations take care to test the response for NSArray or NSDictionary

NSArray * myPeople = [string JSONValue]; 
NSMutableDictionary * organizedData = [[NSMutableDictionary alloc] init];
for (NSDictionary * p in myPeople) {
    [organizedData setValue:p forKey:[p valueForKey:@"id"]];
}
    // now you can query for an id like so
    // [organizedData valueForKey:@"1"]; and your output will be what you wanted from the original question
    // just don't forget to release organizedData when you are done with it
Sign up to request clarification or add additional context in comments.

15 Comments

Hi, thank for reply. I have done it, bt now the problem is that i am getting errors when i put the classes folder in framework folder in my project.
Hi, thank for reply. I have done it, bt now the problem is that i am getting errors when i put the classes folder in framework folder in my project. it gives several errors in streamparser files.
Hey, no problem. What sort of errors? There are several different versions, I would suggest starting at V3.0 and working your way up github.com/stig/json-framework/downloads
I am working on project in which i can display first name and last name in a table row. I am thinking to do this on the basis of their id values and putting them in table rows. Any idea how i can do this, i mean how can i get objects separately.?
@codejunkie. I edited again to help you along. As for the table row. Look up questions on here related to UITableView and good luck. Can you upvote or mark as answered if you feel your original question was resolved. Thanks
|
4

https://github.com/johnezang/JSONKit

I use this to get data from a webservice that spits out 50 records each having another 20 internal elements similar to the one you specify...

I use the JSONKit in the following manner..(Had a look at SBJson a lot of user but i got confused from the word go.)

JSONDecoder *jArray = [[JSONDecoder alloc]init];
NSMutableArray *theObject = [[NSMutableArray alloc] init];   
 theObject = [jArray objectWithData:theResponseData];//objectWithString:theResponseString
NSMutableArray *csArray = [[NSMutableArray array] retain] ;

    for(id key in theObject)
    {
      if([key valueForKey:@"firstName"]  != Nil) 
      {
       ........
       }
     if([key valueForKey:@"lastName"]  != Nil) 
      {
       ........
       }
    }

check it out and let me know if it works or not.. By the way Great responses guys... Good

1 Comment

Hi, Thank you for your reply. But i have already started my project using SBJSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.