【问题标题】:NSString JSON - filtering through dataNSString JSON - 过滤数据
【发布时间】:2015-11-04 22:25:36
【问题描述】:

我正在使用以下代码将 JSON 数据转换为 NSString:

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

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

它以这种格式返回NSString:

数据为字符串

{
    "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”的值。我怎样才能做到这一点?最好存储为双精度。

【问题讨论】:

  • 为什么不将其作为对象返回并在将其变为字符串之前获取值

标签: ios objective-c json string nsstring


【解决方案1】:

与其将其转换为 NSString 不如尝试在 NSDictionary 中使用

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

然后您可以检索USDGBP 的值

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

【讨论】:

    【解决方案2】:
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    double usdgbpValue = [jsonDict[@"quotes"][@"USDGBP"] doubleValue];
    

    【讨论】:

      【解决方案3】:

      您可以在字符串中找到值,只需在 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];
      

      【讨论】:

      • 是的,但这和有效 JSON 字符串中的 NSScanner 一样愚蠢
      【解决方案4】:

      你应该使用 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 解析字符串 ;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多