【问题标题】:Use of undeclared identifier in json for iOS在 iOS 的 json 中使用未声明的标识符
【发布时间】:2011-09-30 14:07:52
【问题描述】:

我在我的 json 中使用了未声明的标识符错误,但我正在遵循来自 http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c 的示例

我该如何解决这个问题?是的,我对 Objective-c \ ios 很陌生 :) 谢谢

我将此代码放在我的 viewcontroller.m 文件中的基于视图的应用程序中

问题在于“SBJSON *parser = [[SBJSON alloc] init];”

 // Create new SBJSON parser object
   SBJSON *parser = [[SBJSON alloc] init];

// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]];

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];

// Each element in statuses is a single status
// represented as a NSDictionary
for (NSDictionary *status in statuses)
{
    // You can retrieve individual values using objectForKey on the status NSDictionary
    // This will print the tweet and username to the console
    NSLog(@"%@ - %@", [status objectForKey:@"text"], [[status objectForKey:@"user"] objectForKey:@"screen_name"]);
}

【问题讨论】:

  • 你记得导入SBJSON库吗?
  • 是的。我添加了 classes 文件夹和 #import SBJson.h 中的所有文件,所以不确定发生了什么。

标签: iphone objective-c json ipad


【解决方案1】:

尝试将行改为SBJsonParser *parser = [[SBJsonParser alloc] init];

编辑:

一种更简单的方法是使用 SBJSON 提供的 NSString 类别:

NSArray *statuses = (NSArray *)[json_string JSONValue];

【讨论】:

  • 感谢现在有效我正在尝试将代码修改为我的 url 并仅打印数组中的一项。 NSLog(@"Message: %@", [status objectForKey:@"message"]);工作吗?
  • 你知道我真的应该为一个新问题开始一个新线程。
猜你喜欢
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多