【问题标题】:Show JSON content depending on user id iOS根据用户 ID iOS 显示 JSON 内容
【发布时间】:2014-01-22 17:58:53
【问题描述】:

我想根据用户 ID 显示内容。 用户登录App,登录成功后会看到从连接外部mysql数据库的JSON文件中取出的id对应的内容。

- (void)viewDidLoad
{
 [super viewDidLoad];
 NSURL *jsonURL = [NSURL URLWithString:@"http://api.example.com/page.php?user_id=1"];
 NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
 NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);
self.something = [dataDictionary objectForKey:@"something"];
}

我的问题是:

如何使(user_id=1)中的“1”动态化,在某种程度上,当用户登录到他的帐户时它会自动更改。

【问题讨论】:

  • 你只是在问如何动态创建URL字符串?
  • 其实我是iOS开发的新手,根据我的需要,我认为我的问题很清楚。所以,如果你能帮助我,你会为我节省很多时间!还是谢谢!

标签: mysql ios objective-c json


【解决方案1】:

动态准备网址

NSString *userID = @"23";//Get your userID after login
NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.example.com/page.php?user_id=%@",userID];

【讨论】:

    【解决方案2】:

    您可以使用StringWithFormat NSString 类的方法,该方法可以帮助您创建动态的NSString 对象,

        - (void)viewDidLoad
        {
        [super viewDidLoad];
        NSString userId = @"your user id here";
        NSURL *jsonURL = [NSURL URLWithString:[NSString StringWithFormat:@"http://api.example.com/page.php?user_id=%@",userId]];
        NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
        NSError *error = nil;
        NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
        NSLog(@"%@",dataDictionary);
        self.something = [dataDictionary objectForKey:@"something"];
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 2013-07-04
      相关资源
      最近更新 更多