0
votes

I have a webservice that returns a VALID JSON array but in my code i get the following error.

"Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9b9af50 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}"

I know that this error is due to error in json. but i have checked and validated the response from various sources they say its valid.

This is my objective-C code for JSON

NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:jsonData];

i also used this code to get the dictionary

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];

But when i debug and check the dictionary variable it says "nil"

Here's my json response

[  
   {  
      "id":"182",
      "name":"2 Tone Brown",
      "price":"2099.0000",
      "description":"",
      "image":"http:\/\/www.nnn.com\/images\/2 Tone BG.jpg",
      "images":[  
         {  
            "1":"2 Tone BG 1.jpg"
         },
         {  
            "2":"2 Tone BG 2.jpg"
         },
         {  
            "3":"2 Tone BG 3.jpg"
         },
         {  
            "4":"2 Tone BG 4.jpg"
         },
         {  
            "5":"2 Tone BG 5.jpg"
         }
      ],
      "sizes":[  
         {  
            "id":"16",
            "size":"L",
            "price":"0.0000"
         },
         {  
            "id":"17",
            "size":"XL",
            "price":"200.0000"
         },
         {  
            "id":"15",
            "size":"M",
            "price":"0.0000"
         },
         {  
            "id":"14",
            "size":"S",
            "price":"0.0000"
         },
         {  
            "id":"23",
            "size":"Custom Size",
            "price":"100.0000"
         },
         {  
            "id":"42",
            "size":"Please Select",
            "price":"0.0000"
         }
      ]
   },
   {  
      "id":"293",
      "name":"3 Seam Collar Black",
      "price":"2499.0000",
      "description":"Exclusive  3 Collar <br> <br> This beautiful but simple color combination gives you a great look. <br> <br> Colors: Red\/Black, Green\/Blue Check, Sky Blue",
      "image":"http:\/\/www.nnn.com\/images\/3 Seam Collar Black Main.jpg",
      "images":[  
         {  
            "1":"3 Seam Collar Black 1.jpg"
         },
         {  
            "2":"3 Seam Collar Black 6.jpg"
         },
         {  
            "3":"3 Seam Collar Black 2.jpg"
         },
         {  
            "4":"3 Seam Collar Black 3.jpg"
         },
         {  
            "5":"3 Seam Collar Black 5.jpg"
         },
         {  
            "6":"3 Seam Collar Black 4.jpg"
         }
      ],
      "sizes":[  
         {  
            "id":"17",
            "size":"XL",
            "price":"200.0000"
         },
         {  
            "id":"16",
            "size":"L",
            "price":"0.0000"
         },
         {  
            "id":"15",
            "size":"M",
            "price":"0.0000"
         },
         {  
            "id":"14",
            "size":"S",
            "price":"0.0000"
         },
         {  
            "id":"23",
            "size":"Custom Size",
            "price":"100.0000"
         }
      ]
   }
]

The above JSON works fine in java but does not produce any results in this.

2
The JSON you posted isn't valid.duci9y
Your data is an RTF file??Anbu.Karthik
I validated the JSON from jsonlint.com it says validuser3879669
The <br> tags disappeared because you did not format the post correctly. Yep, it's valid now.duci9y
What are the exact start characters of jsonData? Could it contain a byte order mark?Joachim Isaksson

2 Answers

1
votes

As your JSON is surrounded by [...], it will return an array, not a dictionary.

1
votes

Removed the

<br>

tags from the response. As i looked into it the dictionary crashes as it started to read

<br>