2

I know a lot of questions have addressed this issue but I have not found one that helped me...

When I parse json data downloaded from a localhost (MAMP server), I face the json error 3840 stating invalid value around character 0...

I don't understand why, since my php script with a var_dump on my array displays (an array of array):

array(2) { [0]=> array(5) { ["ID"]=> string(1) "1" ["EDS"]=> string(4) "1000" ["lastname"]=> string(8) "My lastname" ["firstname"]=> string(9) "My firstname" ["dateOfBirth"]=> string(10) "19.12.1975" } [1]=> array(5) { ["ID"]=> string(1) "2" ["EDS"]=> string(4) "1001" ["lastname"]=> string(14) "Smith" ["firstname"]=> string(6) "John" ["dateOfBirth"]=> string(10) "11.11.1111" } }     

...which for me seems to be a valid json array.

When I log the NSMutableData downloaded, it is not null, but something like

76353648 2734b0a9 (+ around fifty like this).

I don't know if it is because the data is not complete, but I don't know how I can keep on analyzing a little further what is going wrong.

If anyone has an idea of what happens (I understand it has to do with a special character not recognized), it would be great.

Thanks a lot!

Edit: Added followup code to the original question:

In

(void)connectionDidFinishLoading:(NSURLConnection *)connection {
id jsonObject = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

if ([jsonObject isKindOfClass:[NSArray class]]) {

    NSArray *deserializedArray = (NSArray *)jsonObject;

    for (NSDictionary *jsonElement in deserializedArray)
        {
        Person *newPersonElement = [[PersonStore sharedStore] createPerson]; // --> what makes the app crash. But this method is working everywhere else...
    // more code omitted
}

I don't know why this initialization crashes here...

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
Trichophyton
  • 625
  • 5
  • 21

1 Answers1

1

Updated answer:

I think now that you have posted the Objective-C code that uses this received JSON, it becomes clear what the actual problem is.

It sounds to me that you are using Core Data (PersonStore) to persist your incoming data.

If you are making Core Data calls from the same thread that connectionDidFinishLoading: is being called on, you are most likely running into a threading issue where Core Data is not happy that you call it from a thread other than the main thread.

Give this a try: in your connectionDidFinishLoading: wrap your code in the following:

dispatch_async(dispatch_get_main_queue(), ^{
    // Include the code here that walks over the incoming JSON
    // and creates new `Person` instances.
});

This will execute everything in that block, on the main thread. Which is generally a good idea for Core Data usage. (Probably even required, check the documentation, there is a special section on Core Data & Threads if I remember correctly).

Very curious if this does the trick.

Old answer:

The output of vardump is not actually valid JSON. You need to use the json_encode() function instead.

You can turn the NSData that you received from the server into a string by doing something like this:

if let s = String(data: data, encoding: NSUTF8StringEncoding) {
    println(s)
}

You did not mention if you work in Swift or Objective-C but the above translates easily to Objective-C.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
  • I use obj-C. The fact is that I use json_encode() but I test with var_dump just to see how it looks like. What is strange is that when I remove the var_dump my mutableData is null. Is it possible since I json_encode() the same array that the one I passed in with var_dump (which looks like a json structure)? I am a bit lost (even though I will try your string conversion advice)... – Trichophyton Mar 07 '15 at 00:30
  • The output of var_dump() goes into the page output. So it would be part of the JSON that you are trying to decode. Can you share your PHP code? – Stefan Arentz Mar 07 '15 at 00:35
  • I put my php script above... It looks like an error must be in my obj-c code since something new happens: my app crashes without the var_dump() (i.e only with the json_encode() – Trichophyton Mar 07 '15 at 17:22
  • Thanks so much! Apparently that did the trick! You are a genius! :-). Thanks for your help, really appreciated!! – Trichophyton Mar 08 '15 at 12:17