【问题标题】:How to parse this如何解析这个
【发布时间】:2012-02-26 15:22:36
【问题描述】:

我正在尝试使用谷歌网址缩短器 goo.gl,目前它将网址发送到 goo.gl 并返回:

{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/kyPI",
 "longUrl": "http://dsfsd.com/"
}

我认为它是 JSON,但由于 ARC,我遇到了麻烦。无论如何我可以用键“id”解析字符串吗?

【问题讨论】:

  • 为什么你会遇到 ARC 和 JSON 的问题?
  • 由于某种原因,大多数 JSON 框架总是与 ARC 发生冲突。
  • NSJSONSerialization 内置于 ios,如果外部框架给您带来麻烦,请使用它。
  • @ChristopherHannah 只需用-fno-objc-arc编译库的源文件...
  • @RichardJ.RossIII 试过了,但仍然显示 ARC 错误并且无法编译。

标签: objective-c json parsing


【解决方案1】:

如果您的应用面向 iOS 5,您可以使用 Apple 的 NSJSONSeralization 类。

(假设您收到的数据称为 theData)

NSError *error=nil;
    id result=[NSJSONSerialization JSONObjectWithData:theData options:
               NSJSONReadingMutableContainers error:&error];

//to retrieve the 'kind' value of the object
    NSLog("Kind: %@",[result objectForKey:@"kind"]);

【讨论】:

    【解决方案2】:

    它是 JSON,你可以使用像 SBJson 这样的框架来解析它。见http://stig.github.com/json-framework/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      相关资源
      最近更新 更多