2015-11-04 54 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