【问题标题】:Print Label text from NSDictionary which have JSON Object从具有 JSON 对象的 NSDictionary 打印标签文本
【发布时间】:2012-07-20 12:31:11
【问题描述】:

我的 json 文件是这样的

[{"id":"1","category":"New Category1","title":"New Product2","description":"Please type a description1","imgurl":"http:\/\/s.wordpress.org\/about\/images\/wordpress-logo-notext-bg.png","spectitle":"Specification","specvalue":"Value"},{"id":"2","category":"Mobile","title":"Samsung","description":"Please type a description","imgurl":"http:\/\/s.wordpress.org\/about\/images\/wordpress-logo-notext-bg.png","spectitle":"Price","specvalue":"100$"}]

我有 4 个标签,必须在各个标签中打印 id、类别、标题、描述。我已经成功解析了整个 json 对象并打印在一个标签中。我需要显示所有具有相应类别的 json 对象以对应标签。

NSString *labelstring =[NSString stringWithFormat:@"%@",[json objectAtIndex:0]];
label1.text=[NSString stringwithformat: objectForKey:@"id"]; 
 label2.text=[NSString stringwithformat:objectForKey:@"category"];
 label3.text=[NSString stringwithformat:objectForKey:@"title"];  
 label4.text=[NSString stringwithformat:objectForKey:@"description"]; 

这里的 json 是 NSArray 的一个变量,具有 URL 信息和那些东西...但是如果我尝试获取单个值我无法得到它..请指导...

【问题讨论】:

  • 如何分配json 变量?你使用json解析器吗?哪一个?
  • 那些 stringWithFormat: 调用是多余的......你不知道吗?

标签: objective-c ios xcode json uilabel


【解决方案1】:
(void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://advantixcrm.com/prj/mitech/index.php/api/appconfig/Mg"]];

    [request setHTTPMethod:@"GET"];

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

    NSError *err;
    NSURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

    NSArray *array=[jsonArray objectForKey:@"info"];

    for (int i=0; i<[array count]; i++) {
        _label.text = [[array objectAtIndex:i]objectForKey:@"restaurant_location"]);
    }
}

【讨论】:

    【解决方案2】:

    你在展示你的真实代码吗? [NSString stringwithformat:objectForKey:@"id"]; 是无效的语法。你没有收到任何错误吗?

    正确的代码:

    // ??? NSString *labelstring = [NSString stringWithFormat:@"%@",[json objectAtIndex:0]];
    NSDictionary *dict = [json objectAtIndex:0];
    label1.text = [dict valueForKey:@"id"]; 
    label2.text = [dict valueForKey:@"category"];
    label3.text = [dict valueForKey:@"title"];  
    label4.text = [dict valueForKey:@"description"];
    

    编辑:我不知道你是否以正确的方式解析你的 JSON,所以这里也是:

    NSData *jsonData = ...
    NSArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    

    【讨论】:

      【解决方案3】:

      如果你想这样使用json,你需要将它解码成NSDictionary,然后你可以在后半部分尝试使用它:

      label1.text = [NSString stringwithFormat:@"%@", [json objectForKey:@"id"]];
      

      或者,如果您只需要 json 中 "id" 标记下的文本,您可以这样做:

      label1.text = [json objectForKey:@"id"];
      

      【讨论】:

        猜你喜欢
        • 2021-05-12
        • 1970-01-01
        • 2020-04-13
        • 1970-01-01
        • 1970-01-01
        • 2013-10-20
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多