2015-11-04 87 views
-1

我在使用以下代码转换JSON数据NSString滤波:的NSString JSON - 通过数据

NSString *json_string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

    NSLog(@"Data AS STRING %@", json_string); 

它以这种格式返回NSString

数据作为STRING

{ 
    "success": true, 
    "terms": "https://currencylayer.com/terms", 
    "privacy": "https://currencylayer.com/privacy", 
    "timestamp": 1446673809, 
    "source": "USD", 
    "quotes": { 
     "USDAED": 3.672993, 
     "USDAFN": 64.980003, 
     "USDALL": 127.164497, 
     "USDAMD": 474.480011, 
     "USDANG": 1.790326, 
     "USDAOA": 135.225006, 
     "USDARS": 9.536025, 
     "USDAUD": 1.398308, 
     "USDAWG": 1.79, 
     "USDAZN": 1.04625, 
     "USDBAM": 1.800851, 
     "USDBBD": 2, 
     "USDBDT": 78.666946, 
     "USDBGN": 1.80115, 
     "USDBHD": 0.37727, 
     "USDBIF": 1562.5, 
     "USDBMD": 1.00005, 
     "USDBND": 1.403198, 
     "USDBOB": 6.899293, 
     "USDBRL": 3.80025, 
     "USDBSD": 0.999335, 
     "USDBTC": 0.002372, 
     "USDBTN": 65.495003, 
     "USDBWP": 10.55195, 
     "USDBYR": 17415, 
     "USDBZD": 1.994983, 
     "USDCAD": 1.315225, 
     "USDCDF": 929.999946, 
     "USDCHF": 0.99331, 
     "USDCLF": 0.024598, 
     "USDCLP": 692.094971, 
     "USDCNY": 6.33525, 
     "USDCOP": 2837.080078, 
     "USDCRC": 534.015015, 
     "USDCUC": 0.99991, 
     "USDCUP": 0.99978, 
     "USDCVE": 101.349503, 
     "USDCZK": 24.907012, 
     "USDDJF": 177.595001, 
     "USDDKK": 6.8657, 
     "USDDOP": 45.400002, 
     "USDDZD": 107.014999, 
     "USDEEK": 14.325002, 
     "USDEGP": 8.029699, 
     "USDERN": 15.279969, 
     "USDETB": 21.022499, 
     "USDEUR": 0.920471, 
     "USDFJD": 2.157498, 
     "USDFKP": 0.648499, 
     "USDGBP": 0.650132, 
     "USDGEL": 2.395014, 
     "USDZWL": 322.355011 
    } 
} 

我需要从该字符串中获得“USDGBP”的价值。我怎样才能做到这一点?最好以双重形式存储。

+1

为什么不会返回它作为一个对象,并获得价值你把它串 – BlackHatSamurai

回答

0
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
double usdgbpValue = [jsonDict[@"quotes"][@"USDGBP"] doubleValue]; 
1

而不是将其转换成的NSString尝试在NSDictionary中转换它使用

NSError *error; 
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 

然后你可以检索的USDGBP值一样

double usdgbp = [jsonDict[@"quotes"][@"USDGBP"] doubleValue]; 
0

您应该使用NSJSONSerialization

NSData *jsonData = your data ... ; 
NSError *theError = nil; 
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&theError]; 
if (theError) { 
    NSLog(@"%@", theError); 
} 
else { 
    double theValue = [jsonDict[@"quotes"][@"USDGBP"] doubleValue]; 

} 

如果您想获得更多乐趣,请使用NSScanner来解析字符串;)

0

您可以在字符串中找到简单地提取2个字符串之间的字符串的值。

NSString *jsonStr = @"long json string"; 

NSRange str1 = [jsonStr rangeOfString:@"\"USDGBP\":"]; 
NSRange str2 = [jsonStr rangeOfString:@","]; 
NSRange strSub = NSMakeRange(str1.location + str1.length, str2.location - str1.location - str1.length); 
NSString *valueForUSDGBP = [jsonStr substringWithRange:strSub]; 
+0

是以前,但是这同样的傻得像'NSScanner'有效的JSON字符串内 – Matz