繁体   English   中英

如何解析JSON响应?

[英]How to parse the JSON response?

我正在一个项目中必须显示Facebook的朋友列表。 我做了所有必要的编码才能得到响应,但是响应如下

 {"data":[{"name":"Ramprasad Santhanam","id":"586416887"},{"name":"Karthik Bhupathy","id":"596843887"},{"name":"Anyembe Chris","id":"647842280"},{"name":"Giri Prasath","id":"647904394"},{"name":"Sadeeshkumar Sengottaiyan","id":"648524395"},{"name":"Thirunavukkarasu Sadaiyappan","id":"648549825"},{"name":"Jeethendra Kumar","id":"650004234"},{"name":"Chandra Sekhar","id":"652259595"}

谁能告诉我如何将名称和ID保存在两个不同的数组中。

任何帮助将不胜感激。

您可以在下面看到html响应如何解析。 在那里我得到了Facebook的朋友。

- (void)fbGraphCallback:(id)sender 

    {

    if ( (fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0) )


 {

            //restart the authentication process.....
    [fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) 
                         andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];

} 
else 

{

    NSLog(@"------------>CONGRATULATIONS<------------, You're logged into Facebook...  Your oAuth token is:  %@", fbGraph.accessToken);
    FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me/friends" withGetVars:nil];// me/feed 
    //parse our json
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *   facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil]; 
    //init array 
    NSMutableArray * feed = (NSMutableArray *) [facebook_response objectForKey:@"data"];
    //  NSMutableArray *recentFriends = [[NSMutableArray alloc] init];
     arr=[[NSMutableArray alloc]init];
    //adding values to array
    for (NSDictionary *d in feed) 
    {
        [arr addObject:d];


    }

    //NSLog(@"array is %@ ",arr);
    [fbSpinner stopAnimating];
    [fbSpinner removeFromSuperview];

    [myTableView reloadData];


}

}

这是您得到的json响应。 因此,您需要一个JSON解析器将此字符串转换为Objective-C对象。 在iOS App中,您可以使用json-framework之类的库。 这个库可以让您轻松地解析JSON并从字典/数组生成JSON(这实际上是JSON的全部组成)。

来自SBJson docs:JSON解析后,您将获得此转换

JSON通过以下方式映射到Objective-C类型:

  • null-> NSNull
  • 字符串-> NSString
  • 数组-> NSMutableArray
  • 对象-> NSMutableDictionary
  • true-> NSNumber的-numberWithBool:是
  • false-> NSNumber的-numberWithBool:NO
  • 最多19位整数-> NSNumber的-numberWithLongLong:
  • 所有其他数字-> NSDecimalNumber

看起来像JSON,而不是HTML。 (您可能已经知道这一点,因为您用我看到的json标记了问题。)

我不太确定为什么其他人推荐第三方库来执行此操作,除非您需要支持相当旧的OS版本。 只需使用Apple内置的NSJSONSerialization类。

这不是HTML。 这是JSON。 您将需要一个JSON解析器

JSON解析器通常会从字符串中生成NSDictionary或NSArray。 使用我的实现,您将执行以下操作:

NSMutableArray *names = [NSMutableArray array];
NSMutableArray *ids = [NSMutableArray array];

NSDictionary *root = [responseString parseJson];
NSArray *data = [root objectForKey:@"data"];
for (NSDictionary *pair in data)
{
    [names addObject:[pair objectForKey:@"name"]];
    [ids addObject:[pair objectForKey/@"id"]];
}

iOS的最新版本包含一个新的Foundation类NSJSONSerialization ,它将为您处理任何JSON解析和序列化。

暂无
暂无

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

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