I get JSON of the following format:

{ "http://.randomSiteName.com" : [ "key1 = value1;", "key2 = value2;" ] } 

How to get the values ​​(value1 ... 2), the keys (key1 ... 2) and the site itself from the object? I tried through SwiftyJSON, but there either I catch nil, or the empty values ​​returned by the library in case of unsuccessful parsing.

Is the presentation JSON format correct, maybe because of this it’s impossible to parse through SwifyJSON?

    2 answers 2

    The format looks weird, but is syntactically correct. An example of how to work with such json. Change the first lines by yourself, on them I form your json object.

     NSString* str = @"{\"http://.randomSiteName.com\" :[\"key1 = value1;\", \"key2 = value2;\"]}"; NSData *raw = [str dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *json = [[NSDictionary alloc] init]; NSError *error; json = [NSJSONSerialization JSONObjectWithData:raw options:kNilOptions error:&error]; [json enumerateKeysAndObjectsUsingBlock:^(NSString* url, NSArray *values, BOOL *stop) { // url = http://.randomSiteName.com NSLog(@"%@",url); for (NSString *kv in values) { // kv = "key1 = value1;" , "key2 = value2;" & etc; NSLog(@"%@",kv); } }]; 
    • And on the swift is it really done? - user204104 am
    • Yes of course. The syntax is similar: json.enumerateKeysAndObjectsUsingBlock {(url, values, stop) -> Void in - iosp
    • In this block of values, I get as AnyObject, which does not comply with the sequenceType protocol, and I cannot sort it out, I did not quite understand how to be here, but I found a solution that almost completely, but completely solves my problem - user204104
     func JSONParseDictionary(string: String) -> [String: AnyObject]{ if let data = string.dataUsingEncoding(NSUTF8StringEncoding){ do { if let dictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? [String: AnyObject]{ return dictionary } } catch { print("error") } } return [String: AnyObject]() } let dict = self.JSONParseDictionary(json) for i in dict.keys { print(i) // http://.randomSiteName.com } let array = (dict["http://.randomSiteName.com"]?[0] as? String)?.componentsSeparatedByString("=") for i in array! print(i) // [0]key1, [1]value1 }