0

I'm trying to transform a very simple JSON in to a NSDicationary to then populate my CoreData model with the retrieved data, the problem is after I get my json from the server and try to parse it to a NSDicationary my dictionary just show I have 811 values inside but they all are empry values.

NSDictionary

I have printed my json (NSData) and all the content is there and my error var is null after this point.

This is the piece of code I use to do the parse:

 [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error]

And here is an example of my JSON:

[
    {
        "product": "A",
        "name": "B"
    },
    {
        "product": "B",
        "name": "A"
    }
]

If I print the data using the code below, the result is exactly the same I get from my server:

[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]

After clicking in the "I" (information) icon I got that:

Execution was interrupted, reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector. The process has been returned to the state before expression evaluation.

But can't understand why...

I have already searched for this on stackoverflow and I didn't find any solution to my problem, maybe I'm searching with the wrongs terms.

9
  • 1
    Your JSON is not valid—you need quotation marks around product and name. Commented Jun 11, 2014 at 15:51
  • Sorry was my fault, I just copied from Google Chrome, edited the question to show the right JSON Commented Jun 11, 2014 at 15:55
  • What is the result of printing the object you get from NSJSONSerialization? The debugging popover has not always worked for me. Commented Jun 11, 2014 at 16:07
  • 2014-06-11 17:07:16.895 TestApp[26990:60b] jsonObject=[{"product":"A","name":"B"},{"product":"B","name":"A"}] Commented Jun 11, 2014 at 16:11
  • Did you try NSLog the output? Commented Jun 11, 2014 at 16:17

1 Answer 1

1

You say you are parsing your JSON into an NSDictionary but the top-level data structure of your JSON is an array. Parse your JSON into an NSArray; each element of this array will be an NSDictionary.

Sign up to request clarification or add additional context in comments.

2 Comments

I already tryed that, I just got the same result but with a empty NSArray, but I have only changed my "results" var to type NSArray. I need to change the way I do the parse?
Can't understand why but the first time I tested this I got the same error, after testing a bunch of times this was the solution...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.