简体   繁体   English

在iOS的JSON中使用未声明的标识符

[英]Use of undeclared identifier in json for iOS

I am getting a use of undeclared identifier error in my json but I am following the example from http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c 我在json中使用了未声明的标识符错误,但是我正在遵循http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c中的示例

how do I fix this? 我该如何解决? Yes I am very new to objective-c \\ ios :) Thanks 是的,我对objective-c \\ ios非常陌生:)谢谢

I am putting this code in my view based application in my viewcontroller.m file 我将此代码放在我的viewcontroller.m文件中基于视图的应用程序中

The issue is with "SBJSON *parser = [[SBJSON alloc] init];" 问题在于“ 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"]);
}

Try changing the line to SBJsonParser *parser = [[SBJsonParser alloc] init]; 尝试将行更改为SBJsonParser *parser = [[SBJsonParser alloc] init];

Edit: 编辑:

A way that can be easier is to use the NSString category that SBJSON provides: 一种更简单的方法是使用SBJSON提供的NSString类别:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM