I have the json data were within the first array I main category array with name "Response" within that I have parent category array with name "0" within which I have child category array with name "0" and here is my json format
{"Response":[{"menuname":"Jewellery","menuid":"1","0":[{"catname":"Rings","catid":"1","0":[{"scatname":"Engagement Rings","scatid":"4"},{"scatname":"Wedding Rings","scatid":"5"},{"scatname":"kk","scatid":"35"}]},{"catname":"Pendants","catid":"2","0":[{"scatname":"Office Wear","scatid":"8"}]},{"catname":"Bracelets","catid":"3","0":[{"scatname":"Studded","scatid":"9"}]},{"catname":"Earrings","catid":"6","0":[{"scatname":"Ethnic Jhumkas","scatid":"7"}]},{"catname":"Chain's","catid":"33","0":[]},{"catname":"Jewel","catid":"34","0":[]}]},{"menuname":"Collections","menuid":"2","0":[{"catname":"SOUND OF LOVE","catid":"15","0":[{"scatname":"LOVE BRACELET","scatid":"16"}]},{"catname":"COLORFUL AFFAIR","catid":"17","0":[{"scatname":"Passion ring","scatid":"18"}]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]},{"menuname":"Gold Coin","menuid":"3","0":[{"catname":"SOUND OF LOVE","catid":"15","0":[{"scatname":"LOVE BRACELET","scatid":"16"}]},{"catname":"COLORFUL AFFAIR","catid":"17","0":[{"scatname":"Passion ring","scatid":"18"}]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]},{"menuname":"OFF THE SHELF","menuid":"4","0":[{"catname":"testing from pixel","catid":"13","0":[]},{"catname":"New pixel","catid":"23","0":[]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]}]}
and I want to display in expandable tableview as below
Jewellery
Rings
Engagement Rings
Wedding Rings
kk
Pendants
Ofice Wear
Collections
Sound Of Love
Love bracelet
Colorful Affair
Passion ring
Here is the code I used in viewdidLoad
NSDictionary *pJson;
NSMutableString *postStr = [NSMutableString stringWithString:kURL];
[postStr appendString:[NSString stringWithFormat:#"?tag=%#&id=%#",kCategoryFilter,kPrecious]];
[postStr setString:[postStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:postStr]];
NSLog(#"%#",postStr);
[request setHTTPMethod:#"POST"];
_connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
NSURL *url = [NSURL URLWithString:postStr];
NSData *data = [NSData dataWithContentsOfURL:url];
pJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(#"%#",pJson);
NSMutableArray *arr = [pJson objectForKey:#"Response"];
NSLog(#"%lu",(unsigned long)arr.count);
NSMutableDictionary *dict2 =[[NSMutableDictionary alloc]init];// [arr objectAtIndex:NSIndexPath.row];
[dict2 setObject:arr forKey:#"dictionary1"];
dict2 = [arr objectAtIndex:0];
NSArray *arr1 =[dict2 objectForKey:#"0"];
NSLog(#"%lu",(unsigned long)arr1.count);
NSUInteger y;
for (int i=0;i<arr.count;i++) {
NSString *ring_data = [[arr objectAtIndex:i]objectForKey:#"menuname"];
NSString *id_data = [[arr objectAtIndex:i]objectForKey:#"menuid"];
NSLog(#"AUTHOR: %#",ring_data);
NSLog(#"%#",id_data);
dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
ring_data,#"menuname",id_data,#"menuId",nil];
[myObject addObject:dictionary];
NSMutableArray *arr1 = [dict2 objectForKey:#"0"];
NSLog(#"%lu",(unsigned long)arr1.count);
y = arr.count;
for (NSUInteger i=0;i<arr1.count;i++) {
NSArray *count = [[[arr1 objectAtIndex:i] objectForKey:arr]valueForKey:#"catname"];
NSString *myCount = [NSString stringWithFormat:#"%lu",(unsigned long)[count count]];
NSString *ring_data = [[arr1 objectAtIndex:i]objectForKey:#"catname"];
NSLog(#"AUTHOR: %#",ring_data);
// NSLog(#"%#",catname_data);
dictionary1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
ring_data,#"catname",
nil];
[myObject1 addObject:dictionary1];
}
}
I tried some coding for you.Follow that code and customize where you want to add to array and where to set dictionary.
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
NSArray *array=[jsonArray objectForKey:#"Response"];
for (int i=0;i<[array count];i++)
{
NSMutableDictionary *dict = [array objectAtIndex:i];
NSMutableArray *arrayDictValue = [dict valueForKey:[NSString stringWithFormat:#"%d",0]];
NSString *strMenuName = [NSString stringWithFormat:#"%#",[arrayDictValue valueForKey:#"menuname"]];
NSString *strMenuID = [NSString stringWithFormat:#"%#",[arrayDictValue valueForKey:#"menuid"]];
NSLog(#"The strMenuName is-%#",strMenuName);
NSLog(#"The strMenuID is-%#",strMenuID);
for (int j=0; j<[arrayDictValue count]; i++)
{
NSMutableDictionary *dictInside = [arrayDictValue objectAtIndex:j];
NSArray *arrayInsideDict = [dictInside valueForKey:#"0"];
for (int k =0; k<[arrayInsideDict count]; i++)
{
NSString *strCatName = [NSString stringWithFormat:#"%#",[[arrayInsideDict objectAtIndex:k ]valueForKey:#"catname"]];
NSString *strCatID = [NSString stringWithFormat:#"%#",[[arrayInsideDict objectAtIndex:k ]valueForKey:#"catid"]];
NSLog(#"The strCateName is-%#",strCatName);
NSLog(#"The strCatID is-%#",strCatID);
NSMutableDictionary *dictInArray = [[arrayInsideDict objectAtIndex:0] valueForKey:#"0"];
NSString *strSCatName = [NSString stringWithFormat:#"%#",[dictInArray valueForKey:#"scatname"]];
NSString *strSCatID = [NSString stringWithFormat:#"%#",[dictInArray valueForKey:#"scatid"]];
NSLog(#"the strSCatName is - %#",strSCatName);
NSLog(#"the strSCatID is - %#",strSCatID);
}
}
}
Related
My APP gets a JSON string from a api call JSON string has objects and a array in it. This is what i have done so far but i couldn't get the values from it . advice me please and I'm new to iOS .This is my JSON String :
{
"Id":"0d95a9f6-c763-4a31-ac6c-e22be9832c83",
"Name":"john",
"ProjectName":"project1",
"StartDate":"\/Date(1447200000000)\/",
"Documents":
[{
"Id":"2222a","Name":"book1","ContentType":"application/pdf"
},
{
"Id":"3718e","Name":"Toolbox","ContentType":"application/fillform"
}]
}
Code
NSString *URLString = [NSString stringWithFormat:#"http://mysite/API/Assignments?"];
NSURL *url = [NSURL URLWithString:URLString];
NSData *data=[NSData dataWithContentsOfURL:url];
json=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
assignArray=[[NSMutableArray alloc]init];
for (int i=0; i<json.count; i++) {
NSString *aID=[[json objectAtIndex:i]objectForKey:#"Id"];
NSString *uName=[[json objectAtIndex:i]objectForKey:#"Name"];
NSString *pName=[[json objectAtIndex:i]objectForKey:#"ProjectName"];
//[self initwithUserID:uID userName:uName proName:pName];
// [self retrieveAssignmentDetails:aID];
AssignmentsJson *assignment=[[AssignmentsJson alloc]initwithassignID:aID userName:uName proName:pName];
[assignArray addObject:assignment];
your json is not array so parse like following
NSString *aID = json[#"Id"];
NSString *uName = json[#"Name"];
NSString *pName = json[#"ProjectName"];
NSString *startDate = json[#"StartDate"];
NSArray *documents = json[#"Documents"];
for (NSDictionary *item in documents) {
NSString *itemID = item[#"Id"];
NSString *itemName = item[#"Name"];
NSString *itemContentType = item[#"ContentType"];
}
Your Json Object is NSDictionary. so you can directly get data using valueForKey
NSDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSString *aID = json[#"Id"];
NSString *uName = json[#"Name"];
NSString *pName = json[#"ProjectName"];
for Id,Name and ContentType is inside your array object within your NSDictionary object.
so you can get those values accessing array index.
NSArray *arr = json[#"Documents"];
for (int i=0; i<arr.count; i++) {
NSString *aID=[[arr objectAtIndex:i]objectForKey:#"Id"];
NSString *uName=[[arr objectAtIndex:i]objectForKey:#"Name"];
NSString *cType=[[arr objectAtIndex:i]objectForKey:#"ContentType"];
}
You should learn NSArray and NSDictionary Structure first. it will help you in future. Hope this will help you.
you can do it by following way.
Your Json is in NSDictionary format.
NSData * data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://mysite/API/Assignments?"]];
NSDictionary * dicResponse = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if(dicResponse){
NSString * strID = [dicResponse valueForKey:#"Id"];
NSString * strName = [dicResponse valueForKey:#"Name"];
NSString * strProjectName = [dicResponse valueForKey:#"ProjectName"];
NSString * strDate = [dicResponse valueForKey:#"StartDate"];
NSArray * arrDocuments = [NSArray arrayWithArray:[dicResponse valueForKey:#"Documents"]];
for (int i=0; i< arrDocuments.count; i++) {
NSString * ID=[[arrDocuments objectAtIndex:i]valueForKey:#"Id"];
NSString * Name=[[arrDocuments objectAtIndex:i]valueForKey:#"Name"];
NSString * Type=[[arrDocuments objectAtIndex:i]valueForKey:#"ContentType"];
}
}
I am trying to parse an NSString to NSArray but couldn't get any success. Here is my String: NSString *points = #"[[51.471914582, -0.12274114637],[51.47287707533, -0.12163608379]]";
I want this string mapped to NSArray.
NSArray *tempArray = points;
OK, code golf (untested):
NSString *points = #"[[51.471914582, -0.12274114637],[51.47287707533, -0.12163608379]]";
NSError *error = nil;
NSArray *pointsArray = [NSJSONSerialization JSONObjectWithData:[points dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions
error:&error];
for (NSArray *point in pointsArray) {
NSAssert([point count] == 2, #"Invalid point");
// do thing with point
CGPoint pt = CGPointMake([point[0] floatValue], [point[1] floatValue]);
}
Can you plz try this, May it solve your problem,
NSString *points = #"[[51.471914582, -0.12274114637],[51.47287707533, -0.12163608379]]";
NSArray* arrayOfStrings = [points componentsSeparatedByString:#","];
NSLog(#"%#",arrayOfStrings);
NSMutableArray *copyArray = [arrayOfStrings mutableCopy];
NSMutableString *stringFirst = (NSMutableString *)[copyArray objectAtIndex:0];
NSMutableString *stringLast = (NSMutableString *)[copyArray lastObject];
stringFirst=[stringFirst substringFromIndex:1];
stringLast=[stringLast substringToIndex:stringLast.length-1];
[copyArray replaceObjectAtIndex:0 withObject:stringFirst];
[copyArray replaceObjectAtIndex:(copyArray.count-1) withObject:stringLast];
NSMutableArray *yourarray = [[NSMutableArray alloc] init];
for (int i=0; i<=(copyArray.count/2); i+=2) {
[yourarray addObject:[NSString stringWithFormat:#"%#,%#",[copyArray objectAtIndex:i], [copyArray objectAtIndex:i+1]]];
}
NSLog(#"%#",yourarray);
Happy Coding!!
That string looks like JSON so you can use NSJSONSerialization
NSData * data = [points dataUsingEncoding: NSUTF8StringEncoding];
NSError * error = nil;
NSArray * arrayOfPointArrays = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
I made the coding of getting weather application response.I could not get the exact latitude,longitude and population value.Instead of exact value i am getting response as a null.After that i cant get the other response.Also the response is-> " Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' "
Something i have done wrong in array index format.So anyone help me to get all values?
This is my coding
.M part
-(void)viewDidLoad
{
[super viewDidLoad];
NSMutableURLRequest *request =[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:#"http://api.worldweatheronline.com/free/v1/search.ashx?query=London&num_of_results=3&format=json&key=xkq544hkar4m69qujdgujn7w"]];
[request setHTTPMethod:#"POST"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *dict1 =[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err];
//NSDictionary *dict1a =[dict1 objectForKey:#"JSON"];
NSDictionary *dict2 = [dict1 objectForKey:#"search_api"];
NSArray *array1 =[dict2 objectForKey:#"result"];
for(int i=0;i<[array1 count]; i++)
{
NSDictionary *dict3 =[array1 objectAtIndex:i];
NSArray *array2 =[dict3 objectForKey:#"areaName"];
NSDictionary *dict4 =[array2 objectAtIndex:i];
// NSArray *arr3 =[dict4 objectForKey:#"London"];
//manage.transformName= [NSString stringWithFormat:#"%#",[venueNem objectForKey:#"username"]];
NSString *str1= [NSString stringWithFormat:#"%#",[dict4 objectForKey:#"value"]];
NSLog(#"the response ==%#",str1);
NSArray *array3 =[dict3 objectForKey:#"country"];
NSDictionary *dict5 =[array3 objectAtIndex:i];
NSString *str2 =[NSString stringWithFormat:#"%#",[dict5 objectForKey:#"value"]];
NSLog(#"the response ==%#",str2);
NSString *str3 =[NSString stringWithFormat:#"%#",[dict5 objectForKey:#"latitude"]];
NSLog(#"the response ==%#",str3);
NSString *str4 =[NSString stringWithFormat:#"%#",[dict5 objectForKey:#"longitude"]];
NSLog(#"the response ==%#",str4);
NSString *str5 = [NSString stringWithFormat:#"%#",[dict5 objectForKey:#"population"]];
NSLog(#"the response ==%#",str5);
NSArray *arr4 =[dict3 objectForKey:#"region"];
NSDictionary *dict6 =[arr4 objectAtIndex:i];
NSString *str6 = [NSString stringWithFormat:#"%#",[dict6 objectForKey:#"value"]];
NSLog(#"the response ==%#",str6);
NSArray *arr5 =[dict3 objectForKey:#"weatherUrl"];
NSDictionary *dict7 =[arr5 objectAtIndex:i];
NSString *str7 =[NSString stringWithFormat:#"%#",[dict7 objectForKey:#"value"]];
NSLog(#"the response ==%#",str7);
}
for(int j=0;j<[array1 count];j++)
{
NSDictionary *dict8 =[array1 objectAtIndex:j];
NSArray *arr6 =[dict8 objectForKey:#"areaname"];
NSDictionary *dict9 =[arr6 objectAtIndex:j];
NSString *str8 =[NSString stringWithFormat:#"%#",[dict9 objectForKey:#"value"]];
NSLog(#"the response ==%#",str8);
NSArray *arr7 =[dict8 objectForKey:#"country"];
NSDictionary *dict10 =[arr7 objectAtIndex:j];
NSString *str9 =[NSString stringWithFormat:#"%#",[dict10 objectForKey:#"value"]];
NSLog(#"the response ==%#",str9);
NSString *str10 =[NSString stringWithFormat:#"%f",[dict10 objectForKey:#"latitude"]];
NSLog(#"the response ==%#",str10);
NSString *str11 =[NSString stringWithFormat:#"%f",[dict10 objectForKey:#"longitude"]];
NSLog(#"the response ==%#",str11);
NSString *str12 =[NSString stringWithFormat:#"%d",[dict10 objectForKey:#"population"]];
NSLog(#"the response ==%#",str12);
NSArray *arr8 =[dict8 objectForKey:#"region"];
NSDictionary *dict11 =[arr8 objectAtIndex:j];
NSString *str13 =[NSString stringWithFormat:#"%#",[dict11 objectForKey:#"value"]];
NSLog(#"the response ==%#",str13);
NSArray *arr9 =[dict8 objectForKey:#"weatherurl"];
NSDictionary *dict12 =[arr9 objectAtIndex:j];
NSString *str14 =[NSString stringWithFormat:#"%#",[dict12 objectForKey:#"value"]];
NSLog(#"the response ==%#",str14);
}
NSDictionary *dict13 =[array1 objectAtIndex:2];
NSArray *arr10 =[dict13 objectForKey:#"areaname"];
NSDictionary *dict14 =[arr10 objectAtIndex:2];
NSString *str15 =[NSString stringWithFormat:#"%#",[dict14 objectForKey:#"value"]];
NSLog(#"the response ==%#",str15);
NSArray *arr11 =[dict13 objectForKey:#"country"];
NSDictionary *dict15 =[arr11 objectAtIndex:2];
NSString *str16 =[NSString stringWithFormat:#"%#",[dict15 objectForKey:#"value"]];
NSLog(#"the response ==%#",str16);
NSString *str17 =[NSString stringWithFormat:#"%f",[dict15 objectForKey:#"latitude"]];
NSLog(#"the response ==%#",str17);
NSString *str18 =[NSString stringWithFormat:#"%f",[dict15 objectForKey:#"longitude"]];
NSLog(#"the response ==%#",str18);
NSString *str19 =[NSString stringWithFormat:#"%d",[dict15 objectForKey:#"population"]];
NSLog(#"the response ==%#",str19);
NSArray *arr12 =[dict13 objectForKey:#"region"];
NSDictionary *dict16 =[arr12 objectAtIndex:2];
NSString *str20 =[NSString stringWithFormat:#"%#",[dict16 objectForKey:#"value"]];
NSLog(#"the response ==%#",str20);
NSArray *arr13 =[dict13 objectForKey:#"weatherurl"];
NSDictionary *dict17 =[arr13 objectAtIndex:2];
NSString *str21 =[NSString stringWithFormat:#"%#",[dict17 objectForKey:#"value"]];
NSLog(#"the response ==%#",str21); }
Look at you first FOR loop. I am not saying the error is in only there.
EDIT
for(int i=0;i<[array1 count]; i++)
{
NSDictionary *dict3 =[array1 objectAtIndex:i];
NSArray *key = [dict3 allKeys];
for (int j = 0; j < key.count ; j++) {
if ([[key objectAtIndex:j] isEqualToString:#"areaName"]) {
NSArray *array2 =[dict3 objectForKey:#"areaName"];
//Area name
NSString *area = [[array2 objectAtIndex:0] objectForKey:#"value"];
}
else if ([[key objectAtIndex:j] isEqualToString:#"country"]){
NSArray *array2 =[dict3 objectForKey:#"country"];
//Country
NSString *country = [[array2 objectAtIndex:0] objectForKey:#"value"];
}
else if ([[key objectAtIndex:j] isEqualToString:#"latitude"]){
//Latitude
NSString *latitude = [dict3 objectForKey:#"latitude"];
}
else if ([[key objectAtIndex:j] isEqualToString:#"longitude"]){
//longitude
NSString *longitude = [dict3 objectForKey:#"longitude"];
}
else if ([[key objectAtIndex:j] isEqualToString:#"population"]){
//population
NSString *population = [dict3 objectForKey:#"population"];
}
else if ([[key objectAtIndex:j] isEqualToString:#"region"]){
NSArray *array2 =[dict3 objectForKey:#"region"];
//region
NSString *region = [[array2 objectAtIndex:0] objectForKey:#"value"];
}
else if ([[key objectAtIndex:j] isEqualToString:#"weatherUrl"]){
NSArray *array2 =[dict3 objectForKey:#"weatherUrl"];
//weatherUrl
NSString *weatherUrl = [[array2 objectAtIndex:0] objectForKey:#"value"];
}
}
}
arra=[[NSMutableArray alloc]init];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://api.worldweatheronline.com/free/v1/search.ashx?query=London&num_of_results=3&format=json&key=xkq544hkar4m69qujdgujn7w"]];
[request setHTTPMethod:#"GET"];
[request setValue:#"application/json;charset=UTF-8" forHTTPHeaderField:#"content-type"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
NSArray *array=[[jsonArray objectForKey:#"search_api"]objectForKey:#"result"];
for (int i=0; i<[array count]; i++) {
NSLog(#"the areaName==%#",[[[[array objectAtIndex:i]objectForKey:#"areaName"]objectAtIndex:0]objectForKey:#"value"]);
NSLog(#"the country==%#",[[[[array objectAtIndex:i]objectForKey:#"country"]objectAtIndex:0]objectForKey:#"value"]);
NSLog(#"the latitude==%#",[[array objectAtIndex:i]objectForKey:#"latitude"]);
NSLog(#"the long==%#",[[array objectAtIndex:i]objectForKey:#"longitude"]);
NSLog(#"the pop==%#",[[array objectAtIndex:i]objectForKey:#"population"]);
NSLog(#"the region==%#",[[[[array objectAtIndex:i]objectForKey:#"region"]objectAtIndex:0]objectForKey:#"value"]);
NSLog(#"the url==%#",[[[[array objectAtIndex:i]objectForKey:#"weatherUrl"]objectAtIndex:0]objectForKey:#"value"]);
NSString *areaName=[[[[array objectAtIndex:i]objectForKey:#"areaName"]objectAtIndex:0]objectForKey:#"value"];
[arra addObject:areaName];
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
hi i getting json from an external source and parsing it in ios. my code is below
Note = json and cats variables are NSArray;
NSURL * url = [NSURL URLWithString:#"http://myurl.json"];
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
cats = [[NSMutableArray alloc] init];
for (int i=0; i < json.count; i++) {
NSString * CatId = [[json objectAtIndex:i] objectForKey:#"id"];
NSString * CatName = [[json objectAtIndex:i] objectForKey:#"name"];
NSString * CatIcon = [[json objectAtIndex:i] objectForKey:#"icon"];
categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];
[cats addObject:cat];
and json is here
{"categories":[{"id":1,"name":"Healthcare","icon":"/icons/images/65/original_56.png?1386745569"},{"id":10,"name":"Mall","icon":"/icons/images/60/original_51.png?1386745369"},{"id":11,"name":"Taupheq","icon":"/icons/images/23/original_14.png?1386744595"},{"id":12,"name":"Hotel","icon":"/icons/images/27/original_18.png?1386744659"},{"id":13,"name":"SPA","icon":"/icons/images/48/original_39.png?1386745093"},{"id":14,"name":"ATM","icon":"/icons/images/22/original_13.png?1386744578"},{"id":15,"name":"Travel","icon":"/icons/images/12/original_3.png?1386744393"},{"id":16,"name":"Game zone","icon":"/icons/images/68/original_59.png?1386745626"},{"id":17,"name":"Academic","icon":"/icons/images/10/original_1.png?1386744264"},{"id":18,"name":"Textile","icon":"/icons/images/46/original_37.png?1386745050"}]}
Try this:
NSURL * url = [NSURL URLWithString:#"http://myurl.json"];
NSData * data = [NSData dataWithContentsOfURL:url];
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *catsArray = [NSArray arrayWithArray:[json objectForKey:#"categories"]];
cats = [[NSMutableArray alloc] init];
for (int i = 0; i < catsArray.count; i++) {
NSString * CatId = [[catsArray objectAtIndex:i] objectForKey:#"id"];
NSString * CatName = [[catsArray objectAtIndex:i] objectForKey:#"name"];
NSString * CatIcon = [[catsArray objectAtIndex:i] objectForKey:#"icon"];
categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];
[cats addObject:cat];
}
try this:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
json = [dict objectForKey:#"categories"];
When you are trying this:
NSString * CatId = [[json objectAtIndex:i] objectForKey:#"id"];
It translates into: Array -> Dictionary -> String
But you can see you dont want this.
You want: Dictionary -> Array -> Dictionary ->String
In Code:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *array = [dict objectForKey:#"categories"];
NSString * CatId = [[array objectAtIndex:0] objectForKey:#"id"];
try this . . .
NSURL * url = [NSURL URLWithString:#"http://myurl.json"];
NSData * data = [NSData dataWithContentsOfURL:url];
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray * cat = [json objectForKey:#"categories"];
for(NSDictionary * tempdict in cat)
{
NSString * CatId = [NSString stringWithFormat:#"%#",[tempdict objectForKey:#"id"]];
NSString * CatName = [tempdict objectForKey:#"name"];
NSString * CatIcon = [tempdict objectForKey:#"icon"];
categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];
}
THis should work : Consider the Json as NSMutableArray and then easily you ail get the array of the object one by one :
NSMutableArray *userDetails = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
NSLog(#"User Det %#", userDetails);
if (userDetails == nil || [userDetails count] == 0) {
} else {
// 3. iterate the array; each element is a dictionary...
for (NSDictionary *lesson in userDetails)
{
NSString * CatId = [[catsArray objectAtIndex:i] objectForKey:#"id"];
NSString * CatName = [[catsArray objectAtIndex:i] objectForKey:#"name"];
NSString * CatIcon = [[catsArray objectAtIndex:i] objectForKey:#"icon"];
categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];
[cats addObject:cat];
}
}
NSURL * url = [NSURL URLWithString:#"http://myurl.json"];
NSData * data = [NSData dataWithContentsOfURL:url];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *myArray = [jsonData objectForKey:#"categories"];
cats = [[NSMutableArray alloc] init];
for (NSDictionary *temp in myArray) {
NSString * CatId = [NSString stringWithFormat:#"%d",[temp objectForKey:#"id"]];
NSString * CatName = [temp objectForKey:#"name"];
NSString * CatIcon = [temp objectForKey:#"icon"];
categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];
[cats addObject:cat];
}
I have the following JSON!
This JSON wrote my bear drunk vodka :D
{
"Label": [ 1, 2, 3, 4, 5 ],
"ViewId": 1
}
code:
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for (int i=0; i < json.count; i++)
{
NSString * FRid = [[json objectAtIndex:i] objectForKey:#"ViewId"]; //it's work
NSString * FRName = [[json objectAtIndex:i] objectForKey:#"Label"]; //it's don't work Out of scope
How I can get data from "Label" to NSString?
Try:
NSString * FRid = [[json objectAtIndex:i] objectForKey:#"ViewId"];
NSArray * FRName = [[json objectAtIndex:i] objectForKey:#"Label"];
*Label Key contains an array, not a string.
And after this you can convert the array to string by following,
NSString *FRNameString = [FRName componentsJoinedByString:#", "];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *label = [dict objectForKey:#"Label"];
//convert to string
NSString *final = [[NSString alloc]init];
for (NSString * string in label){
final = [NSString stringWithFormat:#"%#%#", final, string];
}
NSLog(#"%#",final);
This is very close pseudocode
I wrote this on my phone, so i can't format as code.