how to fetch value of all source url in array from given string - iphone

{
data = (
{
"created_time" = "2011-09-28T07:29:37+0000";
from = {
id = 100002944043966;
name = "alok sinha";
};
height = 500;
icon = "https://s-static.ak.facebook.com/rsrc.php/v2/yz/r/StEh3RhPvjk.gif";
id = 114750595299741;
images = (
{
height = 2048;
source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s2048x2048/300035_114750595299741_1543248164_n.jpg";
width = 2048;
},
{
height = 500;
source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg";
width = 500;
},
{
height = 500;
source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg";
width = 500;
},
{
height = 480;
source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480/300035_114750595299741_1543248164_n.jpg";
width = 480;
},
{
height = 320;
source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320/300035_114750595299741_1543248164_n.jpg";
width = 320;
},
{
height = 180;
source = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_a.jpg";
width = 180;
},
{
height = 130;
source = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_s.jpg";
width = 130;
},
{
height = 130;
source = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/s75x225/300035_114750595299741_1543248164_s.jpg";
width = 130;
}
);
link = "https://www.facebook.com/photo.php?fbid=114750595299741&set=a.114750591966408.20279.100002944043966&type=1";
picture = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_s.jpg";
position = 1;
source = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/300035_114750595299741_1543248164_n.jpg";
"updated_time" = "2011-09-28T07:29:38+0000";
width = 500;
}
);
paging = {
next = "https://graph.facebook.com/114750591966408/photos?value=1&redirect=1&limit=25&after=MTE0NzUwNTk1Mjk5NzQx";
};
}

NSArray *jsonArray; // your parsed array
NSDictionary *dict = [jsonArray objectAtIndex:0]; // there might be more of the objects and you might need to put that into a forin cycle, but for simplicity this is an example for an array with only one dictionary in it
NSArray *images = [dict objectForKey:#"images"];
NSMutableArray *links = [NSMutableArray array];
for (NSDictionary *img in images) {
[links addObject:[img valueForKey:#"source"]];
}
Now links are the array of NSString objects that you need. You can also transform them into NSURLs if you need to.

Well your array of images is just [jsonObject objectForKey:#"images"] so loop through all of those and take [loopObject objectForKey:#"source"] and lastly, don't forget [jsonObject objectForKey:#"source"]

[yourDictionary objectForKey:#"data"] gives dictionary.
NSDictionary *dictionary = [yourDictionary objectForKey:#"data"]
NSArray *images = [dictionary objectForKey:#"images"];
NSMutableArray *sourceUrls = [[NSMutableArray alloc] initWithCapacity:0];
for (NSDictionary *subDictionary in images]) {
[sourceUrls addObject:[subDictioanary objectForKey:#"source"]];
}
I think it will be helpful to you.

Include SBJSON parser in your code. Then import SBJson.h in the implementation file. Then Use the below code for it.
NSMutableDictionary *responseDictionary = [yourJsonString JSONValue];
NSArray *imageArray = [responseDictionary objectForKey:#"images"];
NSMutableArray *sourceImage=[[NSMutableArray alloc]init]
for(int i=0;i<[imageArray count];i++){
{
[sourceImage appendString:[[imageArray objectAtIndex:i]objectForKey:#"source"]];
}

Related

Dictionary Value retrieve

In my application ,the dictionary value contains following content.How to retrieve the each and store it new array
contryArray(
{
checka0 = Thailand;
checka1 = Brazil;
checka10 = Marocco;
checka11 = Thailand;
checka12 = Jordan;
checka13 = Colombia;
checka14 = Kuwait;
checka3 = Mexico;
checka4 = "Saoudi Arabia";
checka5 = Chili;
checka7 = Australia;
checka8 = Malta;
checka9 = "South Africa";
checkb0 = havana;
checkb1 = Santos;
checkb10 = Casablanca;
checkb11 = Bangkok;
checkb12 = Aqaba;
checkb13 = Havana;
checkb14 = Shuwaikh;
checkb3 = Veracruz;
checkb4 = Jeddah;
checkb5 = "San Antonio";
checkb7 = Maersk;
checkb8 = Maraxklokk;
checkb9 = Durban;
checkc0 = 1;
checkc1 = "0.7";
checkc10 = 1;
checkc11 = "1.2";
checkc12 = 1;
checkc13 = "1.4";
checkc14 = 1;
checkc3 = "0.9";
checkc4 = "0.8";
checkc5 = "0.9";
checkc7 = "2.7";
checkc8 = "0.8";
checkc9 = "0.9";
}
For ex: checka0-checka14 in one array, Here the problem is checka2 and checka6 is not available, Im new bee in xcode,Please help me to retrieve
Your question is not clear. Possibly you need to get all keys in your dictionary and separating each item with specific word in it(checka/checkb/checkc).
If that is the case then,
You will get all the keys using:
NSArray *dictKeys = [yourDictionary allKeys];
You can implement this in multiple ways, one of them:
For storing it in same array:
for (NSString *key in [yourDictionary allKeys])
{
[yourArray addObject:[yourDictionary objectForKey:key]];
}
For storing it in seperate arrays:
for (NSString *key in [yourDictionary allKeys])
{
if([key rangeOfString:#"checka"].location != NSNotFound)
{
//add checka to first array
[yourFirstArray addObject:[yourDictionary objectForKey:key]];
}
else if([key rangeOfString:#"checkb"].location != NSNotFound)
{
//add checkb to second array
[yourSecondArray addObject:[yourDictionary objectForKey:key]];
}
...
}
if contryArray is your dictionary then access it as follow
[contryArray valueForKey:#"checka0"];
and go on adding this values to another array
You can use the following code:
- (void)filterDictionary:(NSDictionary *)dict
{
NSArray *allKeys = [dict allKeys];
NSMutableArray *allValues = [[NSMutableArray alloc]init];
for(id key in allKeys)
{
id value = [dict valueForKey:key];
[allValues addObject:[value stringValue]];
}
}

How to filter array of NSDictionary for key separated by comma

I have an array of NSDictionary.NSDictionary has a key named as multiple_image key that contain string separated by ,.
I want set of array that contain 123.png for multiple_images key.
Can some one show me how to do this using NSPredicate or without predicate.
//Array
{
Image = "<UIImage: 0xf72df30>";
active = yes;
"admin_id" = 169;
"category_id" = 32;
"chef_id" = 175;
descr = "Cool tea to cool down the mind.";
id = 110;
"multiple_images" = "Jellyfish.jpg,345.png";
name = "Southern Sweet Ice Tea";
price = 160;
rating = 3;
selected = 0;
"subcat_id" = 23;
"tag_id" = 45;
"tax_id" = 10;
"tax_value" = "12.00";
},
{
Image = "<UIImage: 0xf72ebd0>";
active = yes;
"admin_id" = 169;
"category_id" = 31;
"chef_id" = 175;
descr = "Ingredients are almonds or cashews. No hydrogenated stuff, no extra weirdo ingredients";
id = 107;
"multiple_images" = "Jellyfish.jpg,123.png";
name = "Butter Chicken";
price = 300;
rating = 3;
selected = 0;
"subcat_id" = 24;
"tag_id" = 43;
"tax_id" = 9;
"tax_value" = "0.00";
},
{
Image = "<UIImage: 0xf72f870>";
active = yes;
"admin_id" = 169;
"category_id" = 31;
"chef_id" = 173;
descr = "Raw vegetables including carrots, cucumbers.";
id = 100;
"multiple_images" = "Jellyfish.jpg,shake.png,";
name = Salads;
price = 50;
rating = 3;
selected = 0;
"subcat_id" = 22;
"tag_id" = 44;
"tax_id" = 9;
"tax_value" = "0.00";
}
Using predicates,
[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"multiple_images CONTAINS '123'"]];
Using predicates, but with blocks
NSArray *filtered = [test filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:
^BOOL(NSDictionary *evaluatedObject, NSDictionary * bindings) {
NSString *key = #"123";
return ([evaluatedObject[#"multiple_images"] rangeOfString:key].location != NSNotFound);
}]];
Try
NSDictionary *item = mainArray[0];
NSString *imagesString = item[#"multiple_images"];
NSArray *images = [imagesString componentsSeparatedByString:#","];
Now you can use the images
Try this code
for (int i=0; i<[mainArray count]; i++) {
NSDictionary *item = [mainArray objectAtIndex:i];
NSString *imagesString = item[#"multiple_images"];
NSArray *images = [imagesString componentsSeparatedByString:#","];
for (int j=0;j<[images count]; j++) {
if ([[images objectAtIndex:j] isEqualToString:#"123.png"]) {
///Your Required code;
}
}
}
So you want to save the dictionary that has 123.png.
maybe you can try something like this:
NSMutableArray *arr = [NSMutableArray new];// your array of objects(dictionaries)
__block NSMutableArray *images123 = [NSMutableArray new];
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableDictionary *dictionary = (NSMutableDictionary *)obj;
if([[dictionary allKeys]containsObject:#"multiple_images"]){
NSString *imageList = (NSString *)[dictionary objectForKey:#"multiple_images"];
NSArray *arrayImages = [imageList componentsSeparatedByString:#","];
if([arrayImages containsObject:#"123.png"]){
[images123 addObject:dictionary];
}
}
}];

Remove objects using NSPredicate

I have the folloving dictionary which has many sub dictionaries.
How can I remove objects where isChanged = 1 from parent dictionary using NSPredicate?
{
"0_496447097042228" = {
cellHeight = 437;
isChanged = 1;
};
"100000019882803_193629104095337" = {
cellHeight = 145;
isChanged = 0;
};
"100002140902243_561833243831980" = {
cellHeight = 114;
isChanged = 1;
};
"100004324964792_129813607172804" = {
cellHeight = 112;
isChanged = 0;
};
"100004324964792_129818217172343" = {
cellHeight = 127;
isChanged = 0;
};
"100004324964792_129835247170640" = {
cellHeight = 127;
isChanged = 1;
};
}
As a simple alternative to using NSPredicate, you can use the NSDictionary's built in keysOfEntriesPassingTest: This answer assumes "isChanged" is an NSString and the value 0 or 1 is an NSNumber:
NSSet *theSet = [dict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
return [obj[#"isChanged"] isEqualToNumber: #1];
}];
The returned set is a list of keys that pass the test. From there, you could remove all that matched with:
[dict removeObjectsForKeys:[theSet allObjects]];
I solved my problem in the following way:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"isChanged == %d", 1];
NSArray *allObjs = [parentDict.allValues filteredArrayUsingPredicate:predicate];
[allObjs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:0];
[keys setArray:[parentDict allKeysForObject:obj]];
[parentDict removeObjectsForKeys:keys];
[keys release];
}];
when you have array of dictionary than you can remove selected category's data using NSPredicate
here is code
NSString *selectedCategory = #"1";
//filter array by category using predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"isChanged == %#", selectedCategory];
NSArray *filteredArray = [yourAry filteredArrayUsingPredicate:predicate];
[yourAry removeObject:[filteredArray objectAtIndex:0]];
But in your problem data is not in array it is in dictionary
your data should be in this format
(
{
cellHeight = 437;
isChanged = 1;
},
{
cellHeight = 145;
isChanged = 0;
},
{
cellHeight = 114;
isChanged = 1;
}
)

convert dictionary to array after removing key values

i need to remove first three keys values from each index of array. What i did is
for (int i=0; i<[normalscoringarray count]; i++)
{
NSDictionary *dictionary = [normalscoringarray objectAtIndex:i];
NSMutableDictionary *mutableDictionary = [dictionary mutableCopy];
[mutableDictionary removeObjectForKey:#"cn"];
[mutableDictionary removeObjectForKey:#"gdate"];
[mutableDictionary removeObjectForKey:#"gid"];
NSLog(#"mutableDictionary.....%#",mutableDictionary);
NSMutableArray *remainingArray = [[NSMutableArray alloc]init];
for (id item in mutableDictionary) {
[remainingArray setArray:[mutableDictionary objectForKey:item]];
}
}
and it does not give remainingArray in that order i want, more over it removes all keys.
normalscoringarray....(
{
cn = "Ranjit Garh";
gdate = "2012-03-25";
gid = 1;
id = 1;
p1nets = "3,3,4,4,2,2,2,4,3,3,4,2,5,3,5,4,3,4||27||33||60";
p1nh = "BP||14||1";
p1score = "4,4,5,5,3,3,3,5,4,4,5,3,5,3,6,4,4,5||36||39||75||61";
p1spoints = "3,3,3,2,3,4,3,3,3,3,2,4,1,2,2,1,3,3||27||21||48";
p2nets = "4,3,4,4,3,4,2,5,4,4,3,3,4,3,6,3,4,4||33||34||67";
p2nh = "DG||18||2";
p2score = "5,4,5,5,4,5,3,6,5,5,4,4,5,4,7,4,5,5||42||43||85||67";
p2spoints = "2,3,3,2,2,2,3,2,2,2,3,3,2,2,1,2,2,3||21||20||41";
p3nets = "4,3,6,4,2,3,2,4,5,4,3,5,6,3,4,3,5,6||33||39||72";
p3nh = "NM||24||3";
p3score = "6,5,7,6,4,5,3,6,6,5,5,6,7,4,5,4,6,7||48||49||97||73";
p3spoints = "2,3,1,2,3,3,3,3,1,2,3,1,0,2,3,2,1,1||21||15||36";
p4nets = "3,4,5,4,2,3,3,5,3,4,3,5,4,3,5,3,4,6||32||37||69";
p4nh = "KS||20||4";
p4score = "5,5,6,5,3,5,4,6,4,5,5,6,5,4,6,4,5,7||43||47||90||70";
p4spoints = "3,2,2,2,3,3,2,2,3,2,3,1,2,2,2,2,2,1||22||17||39";
},
{
cn = "Eden Garden";
gdate = "2012-03-26";
gid = 2;
id = 2;
p1nets = "3,2,5,3,1,2,2,4,4,3,4,3,5,2,5,3,3,5||26||33||59";
p1nh = "NM||24||3";
p1score = "5,4,6,5,3,4,3,6,5,4,5,4,6,3,6,4,4,6||41||42||83||59";
p1spoints = "3,4,2,3,4,4,3,3,2,3,2,3,1,3,2,2,3,2||28||21||49";
p2nets = "2,4,5,3,2,4,3,4,4,4,3,3,5,2,5,3,4,4||31||33||64";
p2nh = "KS||20||4";
p2score = "4,5,6,4,3,6,4,5,5,5,4,4,6,3,6,4,5,5||42||42||84||64";
p2spoints = "4,2,2,3,3,2,2,3,2,2,3,3,1,3,2,2,2,3||23||21||44";
p3nets = "3,4,5,5,3,5,4,5,5,5,4,5,5,4,5,3,5,6||39||42||81";
p3nh = "AK||18||5";
p3score = "4,5,6,6,4,6,5,6,6,6,5,6,6,5,6,4,6,7||48||51||99||81";
p3spoints = "3,2,2,1,2,1,1,2,1,1,2,1,1,1,2,2,1,1||15||12||27";
p4nets = "2,4,5,4,3,3,3,5,5,4,3,5,4,3,5,4,5,6||34||39||73";
p4nh = "KR||20||6";
p4score = "4,5,6,5,4,5,4,6,6,5,4,6,5,4,6,5,6,7||45||48||93||73";
p4spoints = "4,2,2,2,2,3,2,2,1,2,3,1,2,2,2,1,1,1||20||15||35";
},
)
the way i want
normalscoringarray....(
id = 1,
p1nets = "3,3,4,4,2,2,2,4,3,3,4,2,5,3,5,4,3,4||27||33||60",
p1nh = "BP||14||1",
p1score = "4,4,5,5,3,3,3,5,4,4,5,3,5,3,6,4,4,5||36||39||75||61",
p1spoints = "3,3,3,2,3,4,3,3,3,3,2,4,1,2,2,1,3,3||27||21||48",
p2nets = "4,3,4,4,3,4,2,5,4,4,3,3,4,3,6,3,4,4||33||34||67",
p2nh = "DG||18||2",
p2score = "5,4,5,5,4,5,3,6,5,5,4,4,5,4,7,4,5,5||42||43||85||67",
p2spoints = "2,3,3,2,2,2,3,2,2,2,3,3,2,2,1,2,2,3||21||20||41",
p3nets = "4,3,6,4,2,3,2,4,5,4,3,5,6,3,4,3,5,6||33||39||72",
p3nh = "NM||24||3",
p3score = "6,5,7,6,4,5,3,6,6,5,5,6,7,4,5,4,6,7||48||49||97||73",
p3spoints = "2,3,1,2,3,3,3,3,1,2,3,1,0,2,3,2,1,1||21||15||36",
p4nets = "3,4,5,4,2,3,3,5,3,4,3,5,4,3,5,3,4,6||32||37||69";
p4nh = "KS||20||4";
p4score = "5,5,6,5,3,5,4,6,4,5,5,6,5,4,6,4,5,7||43||47||90||70",
p4spoints = "3,2,2,2,3,3,2,2,3,2,3,1,2,2,2,2,2,1||22||17||39",
)
NSMutableArray *remainingArray = [[NSMutableArray alloc]init];
for (int i=0; i<[normalscoringarray count]; i++)
{
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:[normalscoringarray objectAtIndex:i]]; //each time alloc and init with contents of normalscoringarray
//Remove values for keys
[mutableDictionary removeObjectForKey:#"cn"];
[mutableDictionary removeObjectForKey:#"gdate"];
[mutableDictionary removeObjectForKey:#"gid"];
[remainingArray addObject:mutableDictionary]; //add mutableDictionary to remainingArray
[mutableDictionary release]; //free mutableDictionary
}
//finally print contents of remainingArray
NSLog(#"normalscoringarray:\n%#",[remainingArray description]);
Hope this would be help you!
for (int i=0; i<[normalscoringarray count]; i++)
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:[normalscoringarray objectAtIndex:i]];
[dictionary removeObjectForKey:#"cn"];
[dictionary removeObjectForKey:#"gdate"];
[dictionary removeObjectForKey:#"gid"];
[normalscoringarray replaceObjectAtIndex:i withObject:dictionary];
}

Retrieve value from NSDictionnary

I have a NSDictionary and I don't know how I can retrieve the value of "#mode" and "#route" (this is from a json response).
How I can do that?
Thanks!
This is the NSDictionary :
{
"#mode" = WALK;
"#route" = "Rue University";
distance = "17.513516908661757";
duration = 13000;
endTime = "2011-04-29T12:08:59.395-04:00";
from = {
geometry = "{\"type\": \"Point\", \"coordinates\": [-73.57120386807037,45.503820214186405]}";
lat = "45.503820214186405";
lon = "-73.57120386807037";
name = "Rue University";
};
legGeometry = {
length = 3;
points = "{mvtG`k``MOd#ME";
};
startTime = "2011-04-29T12:08:46.395-04:00";
steps = {
walkSteps = (
{
absoluteDirection = NORTHWEST;
becomes = false;
distance = "17.513516908661757";
elevation = "";
lat = "45.503820214186405";
lon = "-73.57120386807037";
stayOn = false;
streetName = "Rue University";
},
{
absoluteDirection = NORTH;
becomes = true;
distance = "0.0";
elevation = "";
lat = "45.50390573910769";
lon = "-73.57139257694809";
relativeDirection = RIGHT;
stayOn = false;
streetName = "street transit link";
}
);
};
to = {
geometry = "{\"type\": \"Point\", \"coordinates\": [-73.571363,45.503971]}";
lat = "45.503971";
lon = "-73.571363";
name = "Station McGill";
stopId = 32;
};
Looks like object at index 0 is a NSDictionary.
NSDictionary *dict = [array objectAtIndex:0];
NSString *route = [dict objectForKey:#"#route"];
Try this:
[myDictionary objectForKey:#"#mode"];
Given an NSDictionary dict, you can retrieve the value corresponding to the key #mode using:
[dict objectForKey:#"#mode"];
Two possibilities:
1) The NSMutableArray is an array of JSON formatted strings.
2) The NSMutableArray is an array of NSDictionaries.
If its #1, then you need to use the Cocoa JSON stuff to decode the JSON string into an NSDictionary:
NSDictionary* aDict = [[myArray objectAtIndex:0] JSONValue];
OR
NSDictionary* aDict = [theArray objectAtIndex:0];
The JSON stuff is available here http://code.google.com/p/json-framework/
Then this should work:
NSString* theMode = [aDict objectForKey:#"#mode"];
NSLog(#"the mode is %#, as read from the dictionary %#", theMode, aDict);
--Tom