When I'm getting request from google maps API i use this method to get json:
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataRequest options: NSJSONReadingAllowFragments error: nil];
jsons look's like this one:
{
routes = (
{
bounds = {
northeast = {
lat = "50.90961";
lng = "20.6492";
};
southwest = {
lat = "50.05775000000001";
lng = "19.94544";
};
};
copyrights = "Dane do Mapy \U00a92012 Google";
legs = (
{
distance = {
text = "126 km";
value = 126118;
};
duration = {
text = "2 godz. 0 min";
value = 7229;
};
"end_address" = "Aleja Powstania Warszawskiego 15, 30-001 Krak\U00f3w, Polska";
"end_location" = {
lat = "50.06";
lng = "19.9589";
};
"start_address" = "Mas\U0142owska 2, 25-001 Kielce, Polska";
"start_location" = {
lat = "50.88884";
lng = "20.6492";
};
steps = (
{
distance = {
text = "0,1 km";
value = 109;
};
duration = {
text = "1 min";
value = 39;
};
"end_location" = {
lat = "50.88824";
lng = "20.64796";
};
"html_instructions" = "Kieruj si\U0119 <b>Mas\U0142owska</b> na <b>po\U0142udniowy zach\U00f3d</b> w stron\U0119 <b>Szyd\U0142\U00f3wek G\U00f3rny</b>";
polyline = {
points = "gfruHo``}BvBvF";
};
"start_location" = {
lat = "50.88884";
lng = "20.6492";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "0,2 km";
value = 221;
};
duration = {
text = "2 min";
value = 97;
};
"end_location" = {
lat = "50.88896";
lng = "20.64503";
};
"html_instructions" = "Skr\U0119\U0107 <b>w prawo</b> w <b>Szyd\U0142\U00f3wek G\U00f3rny</b>";
polyline = {
points = "obruHwx_}BaCpOMv#";
};
"start_location" = {
lat = "50.88824";
lng = "20.64796";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "0,6 km";
value = 596;
};
duration = {
text = "1 min";
value = 56;
};
"end_location" = {
lat = "50.88427";
lng = "20.64091";
};
"html_instructions" = "Skr\U0119\U0107 <b>w lewo</b> w <b>Warszawska</b>";
polyline = {
points = "_gruHmf_}BrGvFjQ`OhA|#";
};
"start_location" = {
lat = "50.88896";
lng = "20.64503";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "1,0 km";
value = 950;
};
duration = {
text = "2 min";
value = 104;
};
"end_location" = {
lat = "50.88779";
lng = "20.62863";
};
"html_instructions" = "Skr\U0119\U0107 w drug\U0105 <b>w prawo</b> w kierunku <b>Jesionowa</b>";
polyline = {
points = "uiquHul~|BiEvScCzKs#xCmBhK_#dD}#dIk#tBYr#Mf#Op#MnA";
};
"start_location" = {
lat = "50.88427";
lng = "20.64091";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "4,2 km";
value = 4185;
};
duration = {
text = "5 min";
value = 292;
};
"end_location" = {
lat = "50.90806000000001";
lng = "20.58021";
};
"html_instructions" = "Na rondzie zjazd nr <b>1</b> w <b>\U0141\U00f3dzka</b>";
polyline = {
points = "u_ruH}_||BCd#Cf#?N#RDPHJJHsBzQIl#OrAm#bEEV[vBa#fBUfA_A~Bk#jA}DvGyDpEu#fAsA`C_AhBeAlCmAhDgAnDuAvEwAzFyAvGgA|F]bDH`Ba#fCQjA]tBi#hC}#zDsB|GeBdDwD|Gg#~#cAhBGJ_H`Mo#jAiB`DaAlBcBfD_#`A[fACDuCrMcC`L?#{AhHw#rDiDhPSx#e#dBc#vAa#fAi#jAgCfF{#rAe#p#A#m#p#kBhB";
};
"start_location" = {
lat = "50.88779";
lng = "20.62863";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "0,2 km";
value = 213;
};
duration = {
text = "1 min";
value = 54;
};
"end_location" = {
lat = "50.90968";
lng = "20.57859";
};
"html_instructions" = "Kontynuuj wzd\U0142u\U017c <b>Trasa 74</b>";
polyline = {
points = "k~uuHiqr|BkBzAiEtEMP";
};
"start_location" = {
lat = "50.90806000000001";
lng = "20.58021";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "92,8 km";
value = 92776;
};
duration = {
text = "1 godz. 17 min";
value = 4641;
};
"end_location" = {
lat = "50.24777";
lng = "20.08501";
};
"html_instructions" = "Skr\U0119\U0107 <b>w lewo</b> w <b>E77</b>";
......
status = OK;
}
Next i use this method to get value for "points" key:
NSArray *items = [jsonArray valueForKeyPath:#"routes.legs.steps.polyline.points"];
When i want to print arrays elements it give only one element (it's strange because i have 25 points in json), and result looks like this one:
(
(
"gfruHo``}BvBvF",
"obruHwx_}BaCpOMv#",
"_gruHmf_}BrGvFjQ`OhA|#",
"uiquHul~|BiEvScCzKs#xCmBhK_#dD}#dIk#tBYr#Mf#Op#MnA",
"u_ruH}_||BCd#Cf#?N#RDPHJJHsBzQIl#OrAm#bEEV[vBa#fBUfA_A~Bk#jA}DvGyDpEu#fAsA`C_AhBeAlCmAhDgAnDuAvEwAzFyAvGgA|F]bDH`Ba#fCQjA]tBi#hC}#zDsB|GeBdDwD|Gg#~#cAhBGJ_H`Mo#jAiB`DaAlBcBfD_#`A[fACDuCrMcC`L?#{AhHw#rDiDhPSx#e#dBc#vAa#fAi#jAgCfF{#rAe#p#A#m#p#kBhB",
"k~uuHiqr|BkBzAiEtEMP",
"ohvuHegr|Bl#rCN`#Xf#n#h#xAx#hAz#nAdCtFdf#~Dv]~Ipu#zHpq#hAjIpA`JhBbJ`CxJjC~IfDlK`AtCfAvCfCdGdGhKpB|CvD`F`D|D~BzBhFbF`DvCjDhC`DdBtFtC|EtBxSjIfAd#|ErBzB~#~InDtX`KjDzArBz#|[dLvYpKzLdF`FnB~E|BrDfCbCxBjClChAjAfAhAbArALPf#r#j#`Ah#dApAfCx#~A~AfDrAlDtAbFrDpOb#rBvBrJj#`Bx#~BlAdDpClEfCrDhF~EhEnD|DvD~BjC~DzFnBfChVj_#|EbIbCvDpCfExY`d#lKbPnE~FfEbEhErCbB|#pBt#zEfAfCXpBHrB?zAK`BUhB[nAa#zAi#|As#p#]l#a#z#i#|AmAhAcAd#e#`AoArAiBdBqChBkDv#gBhBwEXs#Z{#t#sBvDaKnB{E|BmF|AsCbC{DrAkBxBiCnCqCfBwAvCmBTMd#Wv#e#dAg#z#a#hAa#^K\\M~#Y~A]zAWtAQTC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F?D#xAVbBZpE`ArBd#B?nAT~AZ|Bh#pDt#dGpApDv#t#N|#RpB`#dGrAbIxAr#LhATfDl#b#LtBj#\\JRB|#Pj#L~AZvAVh#JhB^lAVbDr#TFrAZlBjBpBrBd#`#BDZZtCbDl#l#xA~AJJXXvA|AJJz#~#tAzA\\`#rAzAbAvAZVLJNL",
"slupHqjxxB\\Xj#^x#j#NHj#\\~A`AVPxBrADBfAt#nAx#dBdAp#d#pAx#dEnCLHLHTNZRRLXPXPfAp#b#Vj#\\|A`AZRj#^|#l#HDrAx#lAz#|#p#FB~#v#pA`AdAv#",
"wmspHw|vxBEDKPALCT?pAAbAFhBNZ",
"ymspH_rvxBd#B`BUdCa#n#[ZOHC",
"ubspHcuvxBDC?Ar#Yl#a#|A_BlC_Dl#k#bAiAd#c#`AcAp#s##Ab#]NILIVKf#Kj#AVCVAr#Id#Ez#SLGDCTORQJK#CZc#R_#b#sAT}#d#sBFWLk#NaAJg#",
"q{qpHe}wxBBMh#cF|#cIXuCv#yGPs#\\y#t#q#",
"mrqpHkayxBlCkAlAi#lAo#v#e#t#e#tC{#^InDu#fAW|#GP#r#BdBZxBn#`IfBlAb#fAv#",
"k~opHceyxBd#j#TTPt#FT",
"u{opHu`yxBoBTu#BuHl#o#BI#",
"klppHg~xxBMqFxAI"
)
)
How can i get a normal NSArray with 25 NSString object?
You're looking at the correct output from the polyline field in the JSON. The data is encoded with Google's polyline encoding, which is documented here
https://developers.google.com/maps/documentation/utilities/polylinealgorithm
There are a number of methods online for parsing said string in objective-c, such as this one.
Ok, i have found the solution parsing json with NSJSONSerialization:
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataRequest options:NSJSONReadingMutableContainers error: nil];
NSArray * items = [jsonArray valueForKeyPath:#"routes.legs.steps.polyline.points"];
items = [items objectAtIndex:0];
items = [items objectAtIndex:0];
And in items array you will get string with encode direction
Related
let newTodo = [
"currentPageNo" : (page.description) ,
"type": "basic_search",
"searchParams": [
"remember_search" : checkk ,
"u_seeking": (bsMatch_looking) ,
"age_from" : (bsMatch_agefrom) ,
"age_to": (bsMatch_ageto),
"u_looking_for_value" :(bsMatch_lookingfoevalue),
"u_country": (bsMatch_country) ,
"u_state" : (bsMatch_ustate) ,
"u_city": (bsMatch_city) ,
"u_postalcode" : (bsMatch_postelcode) ,
"distance": (bsMatch_distance) ,
]
] as [String : AnyObject]
response is below
["type": basic_search , "currentPageNo": 1 ,"searchParams": {
"age_from" = 18;
"age_to" = 44;
distance = 0;
"remember_search" = On;
"u_city" = "";
"u_country" = 1;
"u_looking_for_value" = 0;
"u_postalcode" = "";
"u_seeking" = Female;
"u_state" = California;
}]
But I want this type of response how can I do this only brasses makes the problem for me please provide me the solution
[{ "currentPageNo": 1, "type": basic_search ,"searchParams": {
"age_from" = 20;
"age_to" = 35;
distance = 100;
"remember_search" = On;
"u_city" = London;
"u_country" = 222;
"u_looking_for_value" = "0";
"u_postalcode" = "";
"u_seeking" = Male;
"u_state" = England;
}}]
I want to convert the first code into the second type
You can order them by having keys with type Int.
var myDictionary: [Int: [String: String]]?
I am migrating my data from 1 format to another for a large update to one of my iOS apps. This means that when a user updates their application it migrates their data to a new location. This new location has much stricter rules and uses proper practices (basically when I first created the app I was a noob and there are bad practices currently in the database and this new location is for good data only).
I have everything migrating nicely and everything works except I need to do one last thing. The large dictionary that I save to the database (it uses firebase fyi) has -1.0 stored everywhere there should be a nil value (bad, I know). What I need to do is loop through the entire dictionary and remove any key where the value of that key is -1.0.
The Dictionary is of type [AnyHashable : Any] which is what firebase uses.
I have tried this so far.
if let data = dataDictionary as? [AnyHashable : Any] {
let foundItems = data.filter { $0.value as? Double == -1.0 }
print(foundItems)
}
the plan was to then loop through that found items array and remove any key from the data dictionary that contained it.
This is what the data dictionary looks like:
"-KpIdh_TQMG4fyfFgkdt" = {
assignments = {
"-KpIgH6uN19OpcuedYe1" = {
assignmentGoal = "-1";
assignmentName = "Information System Proposal";
assignmentResult = 100;
assignmentWeight = 5;
};
"-KpIgJnFlC6fhgS0NWxF" = {
assignmentGoal = "-1";
assignmentName = "Information System";
assignmentResult = "-1";
assignmentWeight = 35;
};
"-KpIgOGSAwg_VSpDWhWR" = {
assignmentGoal = "-1";
assignmentName = "Process Analysis";
assignmentResult = "-1";
assignmentWeight = 30;
};
"-KpIgPhu_3Zxw36xt3O4" = {
assignmentGoal = "-1";
assignmentName = Labs;
assignmentResult = "-1";
assignmentWeight = 10;
};
"-KpIgQoFEdRnLlMAq2VN" = {
assignmentGoal = "-1";
assignmentName = Exam;
assignmentResult = "-1";
assignmentWeight = 20;
};
};
paperColor = 22;
paperGoal = 95;
paperName = "Systems Analysis";
};
};
semesterCode = 17S2;
semesterGoal = 90;
semesterName = "2017 Semester Two";
};
};
and this is what it should look like
"-KpIdh_TQMG4fyfFgkdt" = {
assignments = {
"-KpIgH6uN19OpcuedYe1" = {
assignmentName = "Information System Proposal";
assignmentResult = 100;
assignmentWeight = 5;
};
"-KpIgJnFlC6fhgS0NWxF" = {
assignmentName = "Information System";
assignmentWeight = 35;
};
"-KpIgOGSAwg_VSpDWhWR" = {
assignmentName = "Process Analysis";
assignmentWeight = 30;
};
"-KpIgPhu_3Zxw36xt3O4" = {
assignmentName = Labs;
assignmentWeight = 10;
};
"-KpIgQoFEdRnLlMAq2VN" = {
assignmentName = Exam;
assignmentWeight = 20;
};
};
paperColor = 22;
paperGoal = 95;
paperName = "Systems Analysis";
};
};
semesterCode = 17S2;
semesterGoal = 90;
semesterName = "2017 Semester Two";
};
};
The solution I have is to map (iterate) over each parent key and assign it's child nodes to a variable child. Then test each child property (assignmentGoal, assignmentName etc) for either "-1" for the string and -1 for the ints. If it's a match, remove that key: value pair from child.
Then assign child back to the parent node.
let result: [Any] = dict.map { dictionary in
var d = dictionary
var child = d.value as Dictionary
if let goal = child["assignmentGoal"] as? String, goal == "-1" {
child.removeValue(forKey: "assignmentGoal")
}
if let name = child["assignmentName"] as? String, name == "-1" {
child.removeValue(forKey: "assignmentName")
}
if let result = child["assignmentResult"] as? Int, result == -1 {
child.removeValue(forKey: "assignmentResult")
}
if let weight = child["assignmentWeight"] as? Int, weight == -1 {
child.removeValue(forKey: "assignmentWeight")
}
d.value = child
return d
}
//show the output
for item in result {
print(item)
}
and the result output
(key: "-KpIgOGSAwg_VSpDWhWR",
value: ["assignmentName": "Process Analysis System", "assignmentWeight": 30])
(key: "-KpIgQoFEdRnLlMAq2VN",
value: ["assignmentName": "Exam", "assignmentWeight": 20])
(key: "-KpIgJnFlC6fhgS0NWxF",
value: ["assignmentName": "Information System", "assignmentGoal": "3", "assignmentWeight": 35])
(key: "-KpIgPhu_3Zxw36xt3O4",
value: ["assignmentName": "Labs", "assignmentWeight": 10])
(key: "-KpIgH6uN19OpcuedYe1",
value: ["assignmentName": "Information System Proposal", "assignmentResult": 100, "assignmentWeight": 5])
I have a dictionary. When I try to access that dictionary, it is returning a null value when it should definately be returning an actual value. I'm stumped. Any help is always appreciated.
if let d = response.result.value {
print(d) //prints correct data
let prices = d["prices"] as? [[String:AnyObject?]]
print(prices) //prints nil
let best_price = prices?[0]
let price = best_price?["price"] as? String
print(price) //prints nil
}
Console:
{
item = {
"item_number" = 57;
image = "http://example.com/tent.jpg";
name = "Small Red Tent";
};
prices = (
{
link = "http://example.com/id=19";
price = "58.15";
rating = "3.64";
"vendor_id" = 50;
},
{
link = "http://example.com/id=50";
price = "58.14";
rating = "5.00";
"vendor_id" = 110;
},
{
link = "http://example.com/id=26";
price = "50.40";
rating = "4.71";
"vendor_id" = 73;
},
{
link = "http://example.com/id=12";
price = "47.16";
rating = "4.00";
"vendor_id" = 1;
},
{
link = "http://example.com/id=13";
price = "45.75";
rating = "3.90";
"vendor_id" = 25;
},
{
link = "http://example.com/id=16";
price = "41.32";
rating = "3.02";
"vendor_id" = 16;
},
{
link = "http://example.com/id=1";
price = "36.59";
rating = "4.84";
"vendor_id" = 51;
},
{
link = "http://example.com/id=2";
price = "36.29";
rating = "3.26";
"vendor_id" = 43;
},
{
link = "http://example.com/id=13";
price = "34.59";
rating = "4.14";
"vendor_id" = 48;
},
{
link = "http://example.com/id=3";
price = "32.00";
rating = "4.29";
"vendor_id" = 53;
},
{
link = "http://example.com/id=4";
price = "24.50";
rating = "4.16";
"vendor_id" = 8;
},
{
link = "http://example.com/id=5";
price = "15.00";
rating = "4.87";
"vendor_id" = 39;
},
{
link = "http://example.com/id=6";
price = "0.00";
rating = "3.00";
"vendor_id" = 65;
}
);
}
nil
nil
So the solution was proposed by Larme solved the problem.
let prices = d["prices"] as? [AnyObject]
Although I never figured out why casting d["prices"] as? [[String:AnyObject?]] wouldn't also work.
I'm trying to parse a nested JSON output like this in Objective C:
{
-Status: {
code: 200
request: "geocode"
name: "1.304044,103.833867"
}
-Placemark: [
-{
-Point: {
-coordinates: [
103.834
1.30396
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
-Thoroughfare: {
ThoroughfareName: "Bus stop at Lucky Plaza (09048)"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p1"
address: "Bus stop at Lucky Plaza (09048)"
}
-{
-Point: {
-coordinates: [
103.834
1.30444
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
AddressLine: "Lucky Plaza"
-Thoroughfare: {
-PostalCode: {
PostalCodeNumber: "238863"
}
ThoroughfareName: "304 Orchard Road"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p2"
address: "Lucky Plaza, 304 Orchard Road, Singapore 238863"
}
-{
-Point: {
-coordinates: [
103.833
1.30376
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
AddressLine: "Wisma Atria"
-Thoroughfare: {
-PostalCode: {
PostalCodeNumber: "238877"
}
ThoroughfareName: "435 Orchard Road"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p3"
address: "Wisma Atria, 435 Orchard Road, Singapore 238877"
}
-{
-Point: {
-coordinates: [
103.835
1.30389
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
-Thoroughfare: {
-PostalCode: {
PostalCodeNumber: "238860"
}
ThoroughfareName: "291 Orchard Road"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p4"
address: "291 Orchard Road, Singapore 238860"
}
-{
-Point: {
-coordinates: [
103.834
1.30491
0
]
}
-AddressDetails: {
-Country: {
CountryName: "Singapore"
AddressLine: "Kimsia Park"
-Thoroughfare: {
-PostalCode: {
PostalCodeNumber: "228968"
}
ThoroughfareName: "1 Jalan Kayu Manis"
}
CountryNameCode: "SG"
}
Accuracy: 9
}
id: "p5"
address: "Kimsia Park, 1 Jalan Kayu Manis, Singapore 228968"
}
]
}
I have some code working, but I'm finding the nested nature of this to be pretty tough to figure out. What I'd like to get out is an array/dictionary which contains the following elements for each return record: coordinates, CountryName, ThoroughfareName, PostalCode and Accuracy.
Here is my test code so far:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *jsonURL = [NSURL URLWithString:#"http://gothere.sg/maps/geo?output=json&ll=1.304044%2C103.833867&client=&sensor=false&callback="];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
self.jsonArray = [jsonData JSONValue];
NSLog(#"%#", jsonArray);
[jsonURL release];
[jsonData release];
}
My code just manages to get the raw JSON output, any thoughts/code to break it down as I've mentioned above.
Thanks!
As requested, some further information.
Using SBJson framework.
NSLog output from above:
2011-08-30 16:44:32.605 Taxi[53070:207] {
Placemark = (
{
AddressDetails = {
Accuracy = 9;
Country = {
CountryName = Singapore;
CountryNameCode = SG;
Thoroughfare = {
ThoroughfareName = "Bus stop at Lucky Plaza (09048)";
};
};
};
Point = {
coordinates = (
"103.834",
"1.30396",
0
);
};
address = "Bus stop at Lucky Plaza (09048)";
id = p1;
},
{
AddressDetails = {
Accuracy = 9;
Country = {
AddressLine = "Lucky Plaza";
CountryName = Singapore;
CountryNameCode = SG;
Thoroughfare = {
PostalCode = {
PostalCodeNumber = 238863;
};
ThoroughfareName = "304 Orchard Road";
};
};
};
Point = {
coordinates = (
"103.834",
"1.30444",
0
);
};
address = "Lucky Plaza, 304 Orchard Road, Singapore 238863";
id = p2;
},
{
AddressDetails = {
Accuracy = 9;
Country = {
AddressLine = "Wisma Atria";
CountryName = Singapore;
CountryNameCode = SG;
Thoroughfare = {
PostalCode = {
PostalCodeNumber = 238877;
};
ThoroughfareName = "435 Orchard Road";
};
};
};
Point = {
coordinates = (
"103.833",
"1.30376",
0
);
};
address = "Wisma Atria, 435 Orchard Road, Singapore 238877";
id = p3;
},
{
AddressDetails = {
Accuracy = 9;
Country = {
CountryName = Singapore;
CountryNameCode = SG;
Thoroughfare = {
PostalCode = {
PostalCodeNumber = 238860;
};
ThoroughfareName = "291 Orchard Road";
};
};
};
Point = {
coordinates = (
"103.83499999999999",
"1.30389",
0
);
};
address = "291 Orchard Road, Singapore 238860";
id = p4;
},
{
AddressDetails = {
Accuracy = 9;
Country = {
AddressLine = "Kimsia Park";
CountryName = Singapore;
CountryNameCode = SG;
Thoroughfare = {
PostalCode = {
PostalCodeNumber = 228968;
};
ThoroughfareName = "1 Jalan Kayu Manis";
};
};
};
Point = {
coordinates = (
"103.834",
"1.30491",
0
);
};
address = "Kimsia Park, 1 Jalan Kayu Manis, Singapore 228968";
id = p5;
}
);
Status = {
code = 200;
name = "1.304044,103.833867";
request = geocode;
};
}
Try this :
//NSMutableArray *addressDetails = [[NSMutableArray alloc] init];
NSMutableArray *addressDetails = [[jsonArray valueForKey:#"Placemark"] valueForKey:#"AddressDetails"];
//NSMutableArray *point = [[NSMutableArray alloc] init];
NSMutableArray *point = [[jsonArray valueForKey:#"Placemark"] valueForKey:#"Point"];
NSLog(#"%#", point);
NSLog(#"%#", [point objectAtIndex:0]);
NSLog(#"%#", addressDetails);
Like wise you can get array of "id" and "address" value also.
To get value of "coordinates" of point array, do this
NSLog(#"%#", [[point objectAtIndex:0] valueForKey:#"coordinates"]);
For AddressDetails/Country/CountryName :
NSLog(#"%#", [[[addressDetails objectAtIndex:0] valueForKey:#"Country"] valueForKey:#"CountryName"]);
Your json is invalid, try to test it on http://jsonviewer.stack.hu/ once it becames valid then only you can use it. You must have used the SBJSON framework to parse it.
{
education = (
{
school = {
id = 108102169223234;
name = psss;
};
type = College;
year = {
id = 142833822398097;
name = 2010;
};
}
);
email = "amvijaycse#gmail.com";
"first_name" = Vijay;
gender = male;
id = 100000782204693;
"last_name" = Kumar;
link = "http://www.facebook.com/profile.php?id=100000782204693";
locale = "en_US";
location = {
id = 106377336067638;
name = "Bangalore, India";
};
name = "Vijay Kumar";
timezone = "5.5";
"updated_time" = "2010-11-21T07:45:11+0000";
}
i needed email,firstname,lastname.
[EDIT: Removed because it was inaccurate]