user4497445
user4497445

Reputation: 1

Handling Collection data in NSMutable Array in Objective C

I am getting "JSON Data" from "WebServices" and setting them in NSMutableArray and trying to get to show in a Page, I am using "4 Keys" as per "JSON Data" but I don't Know how to use "Key-value" to get particular data from the NSMutableArray, My "JSON` Data" is like this -

More

3 of 5,641

JSON ARRAY FORMAT Inbox x anuj jha

9:13 PM (14 hours ago)

to me, Gaurav, RAjeev

<__NSArrayM 0x7f9e42556c10>(

{

    ansDesc = "Researching a lead that I previously submitted ";

    ansId = 1;

    qsDesc = "What is your primary goal for visiting the portal, today? ";

    qsId = 1;

},

{

    ansDesc = "Submitting a new lead ";

    ansId = 2;

    qsDesc = "What is your primary goal for visiting the portal, today? ";

    qsId = 1;

},

{

    ansDesc = "Working on a draft lead ";

    ansId = 3;

    qsDesc = "What is your primary goal for visiting the portal, today? ";

    qsId = 1;

},

{

    ansDesc = "Daily ";

    ansId = 4;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

{

    ansDesc = "Several times a week ";

    ansId = 5;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

{

    ansDesc = "Once a week ";

    ansId = 6;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

{

    ansDesc = "Every couple of weeks ";

    ansId = 7;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

{

    ansDesc = "About once a month ";

    ansId = 8;

    qsDesc = "How often do you use the Target Revenue Portal? ";

    qsId = 2;

},

I have to show only set of question and corresponding answers of "qsId 1" and "ansId 1, 2, 3" together in the page and then similarly for "qsId 2" with "ansId 4,5,6,7,8" in the next page. Please help me to figure out the most feasible way.

Thanks

Upvotes: 0

Views: 41

Answers (2)

ko100v.d
ko100v.d

Reputation: 1156

Try typing the following code in your program:

NSMutableArray yourData = [NSMutableArray alloc]init];
for (NSDictionary *dic in jSON) {
   [yourData addObject:dic];
}

Now you can accses the value by key, no object by key:

someLabel.text = [yourData valueForKey:ansDesc]; // just an example

Upvotes: 0

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

In Objective C, NSMutableArray doesn't support key-value concept. NSDictionary does support it.

The better way in my mind is using a model class to hold the four properties and use the NSPredicate to filter them out.

@interface JSON_Info : NSObject

//id's
@property (nonatomic, assign) int qsId;
@property (nonatomic, assign) int ansId;

//descriptions
@property (nonatomic, strong) NSString *qsDesc;
@property (nonatomic, strong) NSString *ansDesc; 

@end

If you want to filter it, use NSPredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"qsId == 1 && ansId == 2"];
NSArray *filteredCollection = [self.JSONInfoCollection filteredArrayUsingPredicate: predicate];

Upvotes: 1

Related Questions