I'm in need take the data that passes facebook me with the scores of the players, but I can not return the values that are within the braces in xcode.
Example:
{
"data": [
{
"user": {
"id": "927806543903674",
"name": "Renata Gabi"
},
"score": 333,
"application": {
"name": "Player 2",
"namespace": "quemsoueubiblico",
"id": "303489829840143"
}
},
{
"user": {
"id": "964974026864922",
"name": "Player 1"
},
"score": 230,
"application": {
"name": "My Game",
"namespace": "quemsoueubiblico",
"id": "303489829840143"
}
}
]
}
In android I use this
...
jgame = jObject.getJSONArray("data");
...
score = jgame.getString("score");
name = jgame.getJSONObject("user").getString("name");
photo_id = jgame.getJSONObject("user").getString("id");
for ios in xcode, I was trying this, but is not working
myObject = [[NSMutableArray alloc] init];
NSData *jsonSource = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:#"http://www.escoladepsicanalisekoinonia.com/teste/index.html"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
NSString *score_data = [dataDict objectForKey:#"score"];
NSString *name_data = [dataDict objectForKey:#"name"];
NSString *id_data = [dataDict objectForKey:#"id"];
NSLog(#.........);
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
score_data, score,
name_data, name,
id_data, id,
nil];
[myObject addObject:dictionary];
}
I am not able to adjust the "data" facebook graph, and get the subclasses
The data item in your JSON sample is an array, but you're not taking this into account in your code. Try this:
myObject = [[NSMutableArray alloc] init];
NSData *jsonSource = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:#"http://www.escoladepsicanalisekoinonia.com/teste/index.html"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
NSArray *data = [jsonObjects objectForKey:#"data"];
for (NSDictionary *dataDict in data) {
NSString *score_data = [dataDict objectForKey:#"score"];
NSDictionary *user_data = [dataDict objectForKey:#"user"];
NSString *name_data = [user_data objectForKey:#"name"];
NSString *id_data = [user_data objectForKey:#"id"];
NSLog(#.........);
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
score_data, score,
name_data, name,
id_data, id,
nil];
[myObject addObject:dictionary];
}
Related
I am using an API to convert Json Data into Object data,
please visit the API I am using
Please Visit the API page which I am using
Here is the snap of my code with highlighted issues
Issues
and here is the raw code
-(void) retrieveData{
NSURL * url = [NSURLURLWithString:getDataUrl];
NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil];
NSLog(#"JsonArray %#", jsonArray);
//setup yougaArray
yougaArray = [[NSMutableArray alloc] init];
//Loop through our jsonArray
for (int i = 0; i<jsonArray.count; i++)
{
NSString * yId = [[[[jsonArray objectAtIndex:i]objectForKey:#"data"]objectAtIndex:#"categories"]objectForKey:#"id"];
// NSString * yId = [[jsonArray objectAtIndex:i]objectForKey:#"id"];
NSString * yName = [[[[jsonArray objectAtIndex:i]objectForKey:#"data"]objectAtIndex:#"categories"]objectForKey:#"name"];
NSString * yDescription = [[[[jsonArray objectAtIndex:i]objectForKey:#"data"]objectAtIndex:#"categories"]objectForKey:#"description"];
NSString * yImage = [[[[jsonArray objectAtIndex:i]objectForKey:#"data"]objectAtIndex:#"categories"]objectForKey:#"image"];
//Add the city object to our citiesArray
[yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
}
[self.tableView reloadData];
}
Thanks #Lame, I followed your code, as it was giving me 6 to 8 errors, but I understand your code & configured error in that now here is the complete solution which works according to my requirements or (perfect answer according to my asked question)
-(void) retrieveData{
NSURL * url = [NSURL URLWithString:getDataUrl];
NSData * data = [NSData dataWithContentsOfURL:url];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSDictionary *dataJSON = [jsonDict objectForKey:#"data"];
NSArray *allCategoriesJSON = [dataJSON objectForKey:#"categories"];
yougaArray = [[NSMutableArray alloc] init];
for (int i = 0; i < allCategoriesJSON.count; i ++)
{
NSDictionary *aCategoryJSON = [allCategoriesJSON objectAtIndex:i];
NSString *yId = [aCategoryJSON objectForKey:#"id"];
NSString *yName = [aCategoryJSON objectForKey:#"name"];
NSString *yDescription = [aCategoryJSON objectForKey:#"description"];
NSString *yImage = [aCategoryJSON objectForKey:#"image"];
[yougaArray addObject:[[Youga alloc] initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
}
[self.tableView reloadData];
According to your JSON response its a Dictionary type object not an array so your code should be like this,
NSMutableDictionary *dictData = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil];
NSLog(#"JsonArray %#", dictData);
NSArray *jsonArray=[[dictData objectForKey:#"data"] objectForKey:#"categories"];
//setup yougaArray
yougaArray = [[NSMutableArray alloc] init];
for (int i = 0; i<jsonArray.count; i++)
{
NSString * yId = [[jsonArray objectAtIndex:i] objectForKey:#"id"];
// NSString * yId = [[jsonArray objectAtIndex:i]objectForKey:#"id"];
NSString * yName = [[jsonArray objectAtIndex:i] objectForKey:#"name"];
NSString * yDescription = [[jsonArray objectAtIndex:i] objectForKey:#"description"];
NSString * yImage = [[jsonArray objectAtIndex:i] objectForKey:#"image"];
//Add the city object to our citiesArray
[yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
}
[self.tableView reloadData];
Hope it works for you. Let me know!!
Happy coding. :)
You JSON seems like this :
{
"meta": {
"status": "200",
"msg": "OK"
},
"data": {
"total_pages": 0,
"total_categories": 2,
"current_page": 1,
"next_page": 0,
"categories": [{
"id": "2",
"name": "Articles",
"description": "Yoga Articles",
"image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
}, {
"id": "1",
"name": "Poses",
"description": "Yoga Poses",
"image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
}]
}
}
now replace your existing code with the code below:
NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil];
NSLog(#"JsonArray %#", jsonArray);
//setup yougaArray
yougaArray = [[NSMutableArray alloc] init];
//Loop through our jsonArray
NSArray *dataArray = [[jsonArray objectForKey#"data"] objectForKey:#"categories"];
for (int i = 0; i < dataArray.count; i++) {
NSString * yId = [[dataArray objectAtIndex:i] objectForKey:#"id"];
NSString * yName = [[dataArray objectAtIndex:i] objectForKey:#"name"];
NSString * yDescription = [[dataArray objectAtIndex:i] objectForKey:#"description"];
NSString * yImage = [[dataArray objectAtIndex:i] objectForKey:#"image"];
//Add the city object to our citiesArray
[yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
}
[self.tableView reloadData];
Let me know if the solution works for you, also if anything comes up.
This is the JSON:
{
"meta": {
"status": "200",
"msg": "OK"
},
"data": {
"total_pages": 0,
"total_categories": 2,
"current_page": 1,
"next_page": 0,
"categories": [{
"id": "2",
"name": "Articles",
"description": "Yoga Articles",
"image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
}, {
"id": "1",
"name": "Poses",
"description": "Yoga Poses",
"image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
}]
}
}
Your JSON is a NSDictionary at top level! Not a NSArray!
Also, avoid doing all objectForKey:/objectAtIndex: in the same line/instruction, it's harder to read, but also harder to debug, especially when you don't know what you are doing.
Also, when there is an error parameter, use it, don't put nil.
So:
NSError *errorJSON = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errorJSON];
if (errorJSON)
{
NSLog(#"ErrorJSON: %#", errorJSON);
return;
}
NSDictionary *dataJSON = [jsonDict objectForKey:#"data"];
NSArray *allCategoriesJSON = [dataJSON objectForKey:#"categories"];
for (NSUIInteger i = 0; i < allCategoriesJSON.count; i ++)
{
NSDictionary *aCategoryJSON = [allCategoriesJSON objectAtIndex:i];
NSString yID = [aCategoryJSON objectForKey:#"id"];
NSString yName = [aCategoryJSON objectForKey:#"name"];
NSString yDescription = [aCategoryJSON objectForKey:#"description"];
NSString yImage = [aCategoryJSON objectForKey:#"image"];
[yougaArray addObject:[[Youga alloc] initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
}
IOS newb here having trouble with debugging.
Am trying to handle a json feed but code below is breaking at
- (void)viewDidLoad {
[super viewDidLoad];
shnote = #"shnote”;
lnote = #"lnote”;
myObject = [[NSMutableArray alloc] init];
self.title=#"Challenges";
NSData *jsonSource = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://www.~~/webservice.php"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
//BREAKS HERE
NSString *shnote_data = [dataDict objectForKey:#"shnote”];
//ABOVE LINE HIGHLIGHTED IN GREEN AT BREAKPOINT
NSString *lnote_data = [dataDict objectForKey:#"lnote”];
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
shnote_data, shnote,lnote_data, lnote,nil];
[myObject addObject:dictionary];
}
/*
*/
}
The line highlighted in console is
dataDict = (NSDictionary *const)#"notes"
notes is name of table but other than that I am clueless.
Would appreciate any suggestions.
Your data source is of the format:
{
"notes": [
{
"row": {
"shnote": <...>,
"lnote": <...>
}
},
{
"row": {
"shnote": <...>,
"lnote": <...>
}
},
<...>
]
}
Steps to fetch each row content should therefore be:
Read value of notes property
Iterate through each row
Read value of row property
Read shnote and lnote properties
You're missing steps 1, 2 and 3. In code:
NSURL *url = [NSURL URLWithString:#"http://www.~~/webservice.php"];
NSData *jsonSource = [NSData dataWithContentsOfURL:url];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonSource options:NSJSONReadingMutableContainers error:nil];
NSDictionary *notes = jsonObject[#"notes"];
for(NSDictionary *note in notes) {
NSDictionary *row = note[#"row"];
NSString *shnote = row[#"shnote"];
NSString *lnote = row[#"lnote"];
NSLog(#"%#, %#", shnote, lnote);
}
This question already has an answer here:
Parsing Json to get all the contents in one NSArray
(1 answer)
Closed 8 years ago.
for the moment I fill in my array directly by a native objective-C code :
Datas *pan1 = [[Datas alloc] initWithTitle:#"My array 1" title:#"Shakespeare's Book" location:#"London"];
Datas *pan2 = [[Datas alloc] initWithTitle:#"My array 2" title:#"Moliere's Book" location:#"London"];
NSMutableArray *datasListe = [NSMutableArray arrayWithObjects:pan1, pan2, nil];
But I want to fill this NSMutableArray by this Json list :
{
"myIndex" : [
{
"name":"My array 1",
"title": "Shakespeare's Book",
"location": "London"
},
{
"name":"My Array 2",
"title": "Moliere's Book",
"location": "Paris"
}
]
}
Anyone have ideas? Thanks much!
This json data can be parse very easily like this.
NSError *e;
NSArray *dic= [NSJSONSerialization JSONObjectWithData: jsondata options: NSJSONReadingMutableContainers error: &e];
NSMutableArray *datasListe = [[NSMutableArray alloc] init];
NSMutableArray *data = [dic objectForKey:#"myIndex"];
//Now you have array of dictionaries
for(NSDictionary *dataDic in data){
NSString *name = [dataDic objectForkey:#"name"];
NSString *title = [dataDic objectForKey#"title"];
NSString *location = [dataDic objectForKey#"location"];
Datas *pan= [[Datas alloc] initWithTitle:name title:title location:location];
[dataList addObject:pan];
}
NSDictionary *firstDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
#"Raja", #"name",
#"Developer", #"title",
#"USA", #"location",
nil];
NSDictionary *secondDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
#"Deepika", #"name",
#"Engieer", #"title",
#"USA", #"location",
nil];
NSMutableArray * arr = [[NSMutableArray alloc] init];
[arr addObject:firstDictionary];
[arr addObject:secondDictionary];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(#"jsonArray as string:\n%#", jsonString);
How can I parse a file of this kind:
{"group":"1"}{"group":"2"}{"group":"3"}
Usually I parse in this way:
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:reloadPath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
// getting the data from inside of "menu"
//NSString *message = (NSString *) [data objectForKey:#"message"];
//NSString *name = (NSString *) [data objectForKey:#"name"];
NSArray *messagearray = [data objectForKey:#"message"];
NSArray *namearray = [data objectForKey:#"name"];
NSDictionary* Dictionary = [NSDictionary dictionaryWithObjects:messagearray forKeys:namearray];
...objects of this king...
{"message":["Besth"],"name":["thgh"]}
...but in the type I want to parse, which is the key and object??
By the way I want to retrieve a list like this: 1, 2, 3, ...
This is not valid JSON. You can validate for example at: http://jsonlint.com
You could rewrite it as valid JSON like so:
{
"some_groups": [
{
"group": "1"
},
{
"group": "2"
},
{
"group": "3"
}
]
}
Then you could extract the data by doing something like this:
NSArray *groups = [data objectForKey:#"some_groups"];
for (NSDictionary *group in groups) {
NSLog(#"group number: %#", [group valueForKey:#"group"]);
}
i'm stuck with a problem!
I have a String with JSON like:
{"UserName":"username","PassWord":"password"}
I build this JSON string with xcode like:
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setValue:self.usernameField.text forKey:#"UserName"];
[jsonDict setValue:self.passwordField.text forKey:#"PassWord"];
NSLog(#"Dict: %#",jsonDict);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"JSON String: %#",jsonString);
NSString *postStr = [NSString stringWithFormat:#"%#",jsonString];
But now i want a string with JSON like:
{ "comp": [
{ "id": "1" },
{ "id": "2" },
{ "id": "3" }
],
"contact": [
{ "mail": "some#email.com", "name": "Name" },
{ "mail": "email#email.com", "name": "Name" }
]
}
But how can i do this? Can somebody help me out?
This will give you comp. Using what you have and this example, you should be able to get contact easily.
NSDictionary *id1 = [NSDictionary dictionaryWithObjectsAndKeys:
#"1", "id", nil];
NSDictionary *id2 = [NSDictionary dictionaryWithObjectsAndKeys:
#"2", "id", nil];
NSDictionary *id3 = [NSDictionary dictionaryWithObjectsAndKeys:
#"3", "id", nil];
NSArray *ids = [NSArray arrayWithObjects:id1, id2, id3, nil];
NSDictionary *comp = [NSDictionary dictionaryWithObjectsAndKeys:ids, #"comp", nil];
Also, check out the following library for easy serialization/deserialization.
JSONKit