-3

I am trying to convert serialized Json string to NSDictionary but not find any solution to convert serialized Json to NSDictionary.

This is my response string

{"id":2,"parent_id":1,"lft":2,"rght":3,"name":"Audio Engineering","images":[{"id":22,"user_id":2,"name":"iStock_000027023404_Small","url":"https:\/\/pbs.twimg.com\/profile_images\/97601593\/Picture_3_400x400.png","alt":"iStock_000027023404_Small","description":"","thumbnail":"a:3:{i:0;a:4:{s:4:\"name\";s:25:\"iStock_000027023404_Small\";s:5:\"width\";i:30;s:6:\"height\";i:30;s:3:\"url\";s:58:\"\/uploads\/2016\/09\/thumb\/small\/iStock_000027023404_Small.jpg\";}i:1;a:4:{s:4:\"name\";s:25:\"iStock_000027023404_Small\";s:5:\"width\";i:90;s:6:\"height\";i:90;s:3:\"url\";s:59:\"\/uploads\/2016\/09\/thumb\/medium\/iStock_000027023404_Small.jpg\";}i:2;a:4:{s:4:\"name\";s:25:\"iStock_000027023404_Small\";s:5:\"width\";i:230;s:6:\"height\";i:230;s:3:\"url\";s:67:\"\/uploads\/2016\/09\/thumb\/medium_251x230\/iStock_000027023404_Small.jpg\";}}","created":"2016-09-07T06:24:09+00:00","modified":"2016-09-07T06:24:09+00:00","_joinData":{"id":12,"category_id":2,"image_id":22}}]}

Which I am trying to convert to NSDictionary. In this value for key Thumbnail is serialized data which I am not able to parse.

I have tried

NSData *data = [[self getJsonnResponse] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
                                                     options:kNilOptions
                                                       error:&error];

if (error)
    NSLog(@"%@",[error description]);
else
    NSLog(@"%@",json);

It throws me error

Error Domain=NSCocoaErrorDomain Code=3840 "Badly formed object around character 291." UserInfo={NSDebugDescription=Badly formed object around character 291.}

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46

4 Answers4

1

character 291 means the character at index 291. on JSON Response

your problem is here , is not a valid part

"thumbnail":"a:3:{i:0;a:4:{s:4:\"name\";s:25:\"iStock_000027023404_Small\";s:5:\"width\";i:30;s:6:\"height\";i:30;s:3:\"url\";s:58:\"\/uploads\/2016\/09\/thumb\/small\/iStock_000027023404_Small.jpg\";}i:1;a:4:{s:4:\"name\";s:25:\"iStock_000027023404_Small\";s:5:\"width\";i:90;s:6:\"height\";i:90;s:3:\"url\";s:59:\"\/uploads\/2016\/09\/thumb\/medium\/iStock_000027023404_Small.jpg\";}i:2;a:4:{s:4:\"name\";s:25:\"iStock_000027023404_Small\";s:5:\"width\";i:230;s:6:\"height\";i:230;s:3:\"url\";s:67:\"\/uploads\/2016\/09\/thumb\/medium_251x230\/iStock_000027023404_Small.jpg\";}}

check once in back end and ask the clear format of JSON

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

This one is wrong json formate and what I see here is may be your web developer put log to check array and dictionary is generation proper or not. Please ask web developer to resolve this issue.

your Thumbnail proposed JSON is like this:

{
    {
        "name": "iStock_000027023404_Small";
        "width": 30;
        "height": 30;
        "url": "/uploads/2016/09/thumb/small/iStock_000027023404_Small.jpg";
    } {
        "name": "iStock_000027023404_Small";
        "width": 90;
        "height": 90;
        "url": "/uploads/2016/09/thumb/medium/iStock_000027023404_Small.jpg";
    } {
        "name": "iStock_000027023404_Small";
        "width": 230;
        "height": 230;
        "url": "/uploads/2016/09/thumb/medium_251x230/iStock_000027023404_Small.jpg";
    }
}

May be it will help you.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
0

once check the json objcet is valid or not before parsing using the help of following method

 NSData *data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
if([NSJSONSerialization isValidJSONObject:data]) {
    // here parse your json data
}
Satyanarayana
  • 1,059
  • 6
  • 16
0

Use below code to convert json to dictionary.

NSError * err;

NSData *data =[jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary * responseDict;

if(data!=nil) {

responseDict = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];

}

return responseDict;

Bhavesh Patel
  • 596
  • 4
  • 17