开发者

Modifying the SBJson framwork for my app

I am using the SBJson framework found at github(brilliant stuff) https://github.com/stig/json-framework/

with example : http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c

This twitter example works great now.

So I change my url and

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"]);
}

to

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:@"message"] objectForKey:@"nationalad"]);
}

so my json on my page has message: and a nationalad: yet I don't get any return or or log print out. These are the only 2 things I have changed.

Any Ideas?

This is for the edit:

 SBJsonParser *parser = [[SBJsonParser alloc] init];

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

// 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:@"message"] objectForKey:@"nationalad"]);
  //  NSLog(@"Message: %@", [status objectForKey:@"message"]);

 }
  // NSDictionary *json = [NSString JSONValue];
  NSLog(@"Status: %@", statuses);     
  // NSArray *items = [statuses valueForKeyPath:@"data.array"];
  //NSLog(@"message : %@", [[items objectAtIndex:1] objectForKey:@"mess开发者_运维问答age"]);

and the server page:

{
'message': "<p style=\"color:#FFFFFF;font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;\">Welcome!<\/p><p style=\"color:#FFFFFF;font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;\">Check out today's Dinner and Lunch specials below!<\/p>",
'nationalad': "<img src='http:\/\/www.mywebpage.com\/images\/national\/fullrz_3_4e81fa75ceba5_mywebpage.JPG'>"
}


That’s not valid JSON — all strings must be inside double quotation marks, including names. If you fix your server so that it outputs

{
"message": "<p style=\"color:#FFFFFF;font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;\">Welcome!<\/p><p style=\"color:#FFFFFF;font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;\">Check out today's Dinner and Lunch specials below!<\/p>",
"nationalad": "<img src='http:\/\/www.mywebpage.com\/images\/national\/fullrz_3_4e81fa75ceba5_mywebpage.JPG'>"
}

(note that both message and nationalad are inside double quotation marks), SBJSON should be able to parse your JSON string.

There’s another issue, though: your server isn’t returning an array — it’s returning a single object instead. Either fix your server code so that it returns an array of objects or, in your client code, parse a single object:

NSDictionary *status = [parser objectWithString:json_string error:nil];

Also, note that by using nil in

NSArray *statuses = [parser objectWithString:json_string error:nil];

you’re effectively telling the JSON parser not to return an error object in case there’s an error. Ignoring errors is usually a bad idea. You could do something like this:

NSError *jsonParseError;
NSArray *statuses = [parser objectWithString:json_string error:&jsonParseError];
if (!statuses) {
    // there's been a parse error; look at jsonParseError
    // for example:
    NSLog(@"JSON parse error: %@", jsonParseError);
}

or this:

NSError *jsonParseError;
NSDictionary *status = [parser objectWithString:json_string error:&jsonParseError];
if (!status) {
    // there's been a parse error; look at jsonParseError
    // for example:
    NSLog(@"JSON parse error: %@", jsonParseError);
}


Firstly, NSLog status and see if it is nil. If it is, then you should check the URL that you are getting the JSON from.

If the URL is nil, then correct the URL and try again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜