So, i have a loop where i repeatedly call an url, which returns contacts in JSON format.
Since i call the url more than once, i want to add it all up, an ultimately end up with a NSDictionary, containing contacts for all the url calls.
What i'm doing now, is appending the data for each iteration:
NSData *response = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
[data appendData:response];
and when the loop is over i try to make it into a NSDictionary:
// Loop over - create Dict from all the data.
NSDictionary *JSONDataFinal = [NSJSONSerialization JSONObjectWithData:data
options:0 error:&error2];
I get an error: "garbage at end" and believe the approach is wrong, since it doesn't know how to put data from each consecutive call into the same key, and not create a key for each call.
Should i create a Dictionary for each iteration - and combine them in the end?
You can append data to a NSMutableDictionary after each call.
NSMutableDictionary* jsonDict = [[NSMutableDictionary alloc] init];
for (...) {
...
NSData *response = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
NSDictionary *JSONData = [NSJSONSerialization JSONObjectWithData:data
options:0 error:&error2];
[jsonDict addEntriesFromDictionary:JSONData];
}
Create different dictionary for each json data and store in main array like this:
NSMutableArray *arrAllContacts = [NSMutableArray array];
//here arrJsonURL which stores all url for contacts
for(int i=0; i<[arrJsonURL count]; i++)
{
//create request
...............
NSData *response = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
[data appendData:response];
NSError *error = nil;
//get dictionary from json data
NSDictionary *dictContact = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
[arrAllContacts addObject:dictContact];
}
Related
I want to try to convert NSData to NSDictionary or NSArray but some how i can't do that but whenever try to convert into NSString it will converted and gating data so any one can know what is mistake ,help me below is code for convert
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
Below code for convert into NSString
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
But Problem is that When get this type of data it will converted on array
Response:
[["superwoman",0],["sugar",0],["sultan trailer",0],["summer",0],["superman",0],["sunday candy",0],["sublime",0],["summer sixteen",0],["superfruit",0],["sultan songs",0]]
but Whenever gat this type of response it will not converted on array formated because of ,[3 on response
Response:
[["ssundee",0],["selena gomez",0],["sia",0],["sorry beyonce",0],["smosh",0],["sweatshirt jacob
sartorius",0],["skydoesminecraft",0],["secret life of pets
trailer",0,[3]]
try this one
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
options:kNilOptions
error:&error];
1)You need to take the response in id type variable & check the class of response like below.
2)You also need to check if JSON convertion is successful
NSError * error;
id result = [NSJSONSerialization JSONObjectWithData:data
options:0 error:&error];
if (!error) {
if([id isKindOfClass:[NSDictionary class]]){
//Response is Dictonary
} else if ([id isKindOfClass:[NSArray class]]){
//Response is Array
} else {
//any other format
}
} else {
//Handle error
}
NSString *connection = #"http:"(link);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:connection];
NSString *json = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSLog(#"\nJSON: %# \n Error: %#", json, error);
if(!error) {
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#"%#",jsonData);
NSMutableArray *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSLog(#"JSON: %#", jsonDict);
}
});
The above code returns :
JSON: "{\"value\":[{\"Id\":\" }}
but
NSData is 69746963 735c222c 5c224973 41767469 76655c22 3a5c2231 5c222c
and jsonDict returns (null).
I am confused what may be going on.
Can somebody please help me figure it out.
I am trying to get the JSON data but it returns null.
Thanks
You say the server gave you this:
JSON: "{\"value\":[{\"Id\":\" }}
If I remove the backslashes that were added by the NSLog command, the actual JSON that you received was
{"value":[{"Id":" }}
That isn't valid JSON. That's all there is to it; the server sent you rubbish data, and there is no way to get any information from it. Contact the people creating the server software and ask them to fix the problem.
And you really, really need to distinguish between what an object really contains, and what NSLog prints. NSLog prints the contents of an NSData object by displaying each byte as two hexadecimal digits.
Try one of these (array or dictionary) without converting it into JSON, depending on what u want :
NSString *connection = #"http://(link)";
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSDictionary *jsonDict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:connection]];
NSArray *jsonArray = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:connection]];
NSLog(#"DICT: %# ARRAY: %#", jsonDict, jsonArray);
});
I haven't tried it, please paste results?
By the look of your NSString response, it starts with { and ends with } which means it is a NSDictionary.
Change :
NSMutableArray *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
to :
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&error];
I have a WebService, which give me the fallowing Json-String back:
"{\"password\" : \"1234\", \"user\" : \"andreas\"}"
I call the webservice and try to parse the returned data like:
[NSURLConnection sendAsynchronousRequest: request
queue: queue
completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
if (error || !data) {
// Handle the error
} else {
// Handle the success
NSError *errorJson = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &errorJson];
NSString *usr = [responseDict objectForKey:#"user"];
}
}
];
But the resulting NSDictionary looks like:
What has the effect, that I cannot get the values - for example user.
Can someone help me, what I am doing wrong? - Thank you.
From the debugger screenshot is seems that the server is (for whatever reason) returning
"nested JSON": responseDict[#"d"] is a string containing JSON data again, so you have to
apply NSJSONSerialization twice:
NSError *errorJson = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData: data options:0 error: &errorJson];
NSData *innerJson = [responseDict[#"d"] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *innerObject = [NSJSONSerialization JSONObjectWithData:innerJson options:NSJSONReadingMutableContainers error:&errorJson];
NSString *usr = [innerObject objectForKey:#"user"];
If you have the option, a better solution would be to fix the web service to return
proper JSON data.
I'm searching Twitter for tweets with this code:
- (void)fetchTweets
{
NSURL *url = [NSURL URLWithString:#"http://search.twitter.com/search.json"];
NSDictionary *params = [NSDictionary dictionaryWithObject:#"#winning" forKey:#"q"];
TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodGET];
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
NSError *jsonError;
tweets =
[NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&jsonError];
if (tweets) {
// NSLog(#"%#", tweets);
}
else {
NSLog(#"%#", jsonError);
}
}
[[self delegate] receivedTweets];
}];
[self performSelector:#selector(fetchTweets) withObject:nil afterDelay:30];
The tweets variable is a NSArray and I am trying to put it into a NSDictionary with this code:
NSDictionary *tweet = [[TwitterHandler sharedInstance].tweets objectAtIndex:indexPath.row];
I am definitely getting the JSON text for the tweets but when trying to add them to the dictionary so I can eventually put them into a table I get the "unrecognized selector sent to instance" error.
I am not sure why I am getting this and any help would be appreciated.
In this code:
if (responseData) {
NSError *jsonError;
tweets =
[NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&jsonError];
if (tweets) {
// NSLog(#"%#", tweets);
}
else {
NSLog(#"%#", jsonError);
}
Is tweets the same that you are getting with this code:
[[TwitterHandler sharedInstance].tweets
If so, it seems to me that you are assigning it to au autoreleased object, so it will eventually be released and you will end-up with a pointer to a released object.
Better retain it or use a retain property when assigning it.
Something like
tweets =
[[NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&jsonError] retain];
or, assuming that tweets is a retain orpperty:
self.tweets =
[NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&jsonError];
Do not forget to release it when it is no longer needed (at least in the dealloc of your class)
EDIT
Ok, here is the thing, and it has nothing to do with memory management (wrong initial guess!): I tried your URL, and it returns:
{"error":"You must enter a query."}
Which is a JSon dictionary. Then the JSON Parser identifies this as a dictionary and returns you a dictionary. You may store it in a NSArray*, but the object instantiated is still a dictionary, and will only responds to the NSDictionary methods. As you comment shows when you print it, it prints a dictionary.
I had this once and it was the app releasing the view controller, check if your view controller is being released.
[NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&jsonError];
This returns an NSDictionary object of the tweets. The Twitter search results from that URL can be filtered out by looking for the content in the key results.
AKA:
NSDictionary *jsonTweet = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&jsonError];
NSArray *tweet = [jsonTweet objectForKey:#"results"];
Then the tweets can be iterated through since they are in an NSArray.
My Json response is
[{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}]
And I did
NSString *response = [request responseString];
SBJSON *parser = [[SBJSON alloc] init];
NSArray *jsonObject = [parser objectWithString:response error:nil];
At this point I believe that jsonObject is a array that has two NSDictionary..
How can I make jsonObject that has two NSArray instead of NSDictionary?
What is the best way to do so? I think that I need to convert nested NSDictionary to NSArray?
I would use the NSJSONSerialization class now in the Foundation framework.
With the JSONObjectWithData method, it will return the type of container you want the data stored in at the top level. For example:
NSError *e;
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e];
for (NSDictionary *dict in jsonObject) {
NSLog(#"json data:\n %#", dict);
// do stuff
}
Alternately, you could return a mutable container, such as:
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e];
Here is the Apple documentation:
NSJSONSerialization