-2

I have a JSON array being output on a page. I would like to convert this JSON array into an NSArray.

enter image description here

Or convert this json like : enter image description here

Can someone let me know what the step might be? Thank you!

Fadielse
  • 381
  • 2
  • 12
  • 2
    `NSJSONSerialization`. This has probably been answered a few million times. – Avi Apr 14 '16 at 08:44
  • @trojanfoe, thanks for finding the duplicate. I knew it had been answered, but I didn't have time for a more comprehensive search when I commented before. – Avi Apr 14 '16 at 08:56
  • 1
    @trojanfoe its not good at all you demotivate a new user. instead of helping him you closed his question. – Abhinandan Pratap Apr 14 '16 at 09:05
  • @Abhi That's what happens to duplicate questions on the stackexchange network. If you don't like it then talk to the admins or stop using the sites. – trojanfoe Apr 14 '16 at 09:09

1 Answers1

1

Try this.

NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:0   error:nil];

Or

NSArray * json = [self JSONWithData:data];

+(id)JSONWithData:(NSData *)data
{
    if (!data)
    {
        return nil;
    }

    id json = [NSJSONSerialization JSONObjectWithData:data
                                              options:0
                                                error:nil];
    if (!json)
    {
        // try to clean response
        NSString * dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        if (!dataString)
        {
            dataString = [[NSString alloc] initWithData:data encoding:NSWindowsCP1251StringEncoding];
        }
        NSRange rangeDict = [dataString rangeOfString:@"{"];
        NSRange rangeArray = [dataString rangeOfString:@"["];
        NSRange range = rangeDict;
        if ((rangeDict.location == NSNotFound) || (rangeArray.location != NSNotFound && rangeArray.location < rangeDict.location))
        {
            range = rangeArray;
        }

        if (range.location != NSNotFound)
        {
            dataString = [dataString substringFromIndex:range.location];
            NSError * error = nil;
            if ([dataString length])
            {
                json = [NSJSONSerialization JSONObjectWithData:[dataString dataUsingEncoding:NSUTF8StringEncoding]
                                                       options:0
                                                         error:&error];
                if (!json)
                {
                    dataString = [dataString stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
                    error = nil;
                    json = [NSJSONSerialization JSONObjectWithData:[dataString dataUsingEncoding:NSUTF8StringEncoding]
                                                           options:0
                                                             error:&error];
                }
                if (!json)
                {
                    dataString = [dataString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
                    error = nil;
                    json = [NSJSONSerialization JSONObjectWithData:[dataString dataUsingEncoding:NSUTF8StringEncoding]
                                                           options:0
                                                             error:&error];
                }
            }
            if (json)
            {
                NSLog(@"Response cleaned");
            }
            else
            {
                NSLog(@"Failed to parse JSON: %@", [error localizedDescription]);
            }
        }
    }

return json;
}

And use it like:

if (json) {
    if ([json count] > 0) {
        NSString * name = json[0]["name"]
        if (name) {

        }
        NSArray *arrayContent = json[0]["array_content"]
    }
}

For Swift 3.0

extension JSONSerialization {
    final class func jsonObject(with data: Data, checkEscapeSymbols: Bool = false) -> Any? {
        do {
            var data = data
            if !data.isEmpty, let txt = String(data: data, encoding: .utf8) {
                var newTxt = txt.replacingOccurrences(of: "\\", with: "\\\\")
                newTxt = newTxt.replacingOccurrences(of: "\\\\/", with: "\\/")
                newTxt = newTxt.replacingOccurrences(of: "\\\\\"", with: "\"")
                newTxt = newTxt.replacingOccurrences(of: "\\\"", with: "\"")
                newTxt = newTxt.replacingOccurrences(of: "\\\\'", with: "\\'")
                if checkEscapeSymbols {
                    newTxt = newTxt.replacingOccurrences(of: "\0", with: "\\0")
                    newTxt = newTxt.replacingOccurrences(of: "\r", with: "\\r")
                    newTxt = newTxt.replacingOccurrences(of: "\n", with: "\\n")
                    newTxt = newTxt.replacingOccurrences(of: "\t", with: "\\t")
                }
                if let data_ = newTxt.data(using: .utf8) { data = data_ }
            }
            return try jsonObject(with: data, options: [])
        } catch { return nil }
    }
}

guard let json = JSONSerialization.jsonObject(with: data) as? AnyObject else { return }
Dmytro Shvetsov
  • 946
  • 10
  • 13