NSJSONSerialization - How to convert JSON into NSArray properly?
-
04-06-2021 - |
Question
I encountered the following problem: I want so connect my iPhone-App with a database on a server. Therefore I use some (simple) .php files to manage the access to the DB. Inserting new data already works but I have some trouble to convert fetched data into a NSMutableArray :
NSURL *contentURL = [NSURL URLWithString:[kHOSTURL stringByAppendingString:kGETBarsURL]];
NSLog(@"URL : %@", contentURL);
NSData *contentData = [NSData dataWithContentsOfURL:contentURL];
NSLog(@"Data : %@", contentData);
NSError *e = nil;
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:contentData
options:kNilOptions
error:&e];
NSLog(@"JSON : %@", jsonArray);
NSLog(@"Error : %@", e);
Output is like this (I 'XX' and shortened 'Data :') :
2012-04-28 13:49:37.229 XX[14434:f803] URL : http://xx/getBars.php
2012-04-28 13:49:37.389 XX[14434:f803] Data : <5b7b2275 6e697175 65223a22 34222c22 4e616d65 223a2254 65737422 2c224465 7461696c 73223a22 54686973 49734154 65737422 7d2c7b22 ...>
2012-04-28 13:49:37.390 XX[14434:f803] JSON : (null)
2012-04-28 13:49:37.392 XX[14434:f803] Error : Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x6daa610 {NSDebugDescription=Garbage at end.}
If I open the page in a browser it looks like this:
[{"unique":"4","Name":"Test","Details":"ThisIsATest"},
{"unique":"5","Name":"Test","Details":"ThisIsATest"},
{"unique":"6","Name":"Test","Details":"ThisIsATest"},
{"unique":"7","Name":"Test","Details":"ThisIsATest"},
{"unique":"8","Name":"Test","Details":"ThisIsATest"},
{"unique":"9","Name":"Test","Details":"ThisIsATest"},
{"unique":"10","Name":"Test","Details":"ThisIsATest"}]
I've also tried other options in NSJSONSerialization but that did not work :( Can anybody help me here ?
2012-04-28 14:18:30.192 XX[14541:f803] Encoding : [{"unique":"4","Name":"Test","Details":"ThisIsATest"},{"unique":"5","Name":"Test","Details":"ThisIsATest"},{"unique":"6","Name":"Test","Details":"ThisIsATest"},{"unique":"7","Name":"Test","Details":"ThisIsATest"},{"unique":"8","Name":"Test","Details":"ThisIsATest"},{"unique":"9","Name":"Test","Details":"ThisIsATest"},{"unique":"10","Name":"Test","Details":"ThisIsATest"}]
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16106315-6']);
_gaq.push(['_setDomainName', '.xx.de']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
Solution
It's pretty obvious that you indeed have 'garbage' at the end. You have a JavaScript block that although is not visible in the browser, it's still returning from your php script. Remove that and you should be good to go.
OTHER TIPS
I had the same problem recently and after almost one hour I found out it was a problem with the URL I was sending the request to. Check the URL to see if it is in fact responding with the JSON data. Good luck!;