将JSON数据解析为标签

时间:2014-01-30 07:01:11

标签: ios objective-c

 reponsedata = [[NSMutableData alloc]init];
    NSString *loc = [NSString stringWithFormat:@"http://advantixcrm.com/prj/mitech/index.php/api/appconfig/Mg"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:loc]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (reponsedata)
    {

        NSDictionary *Dictionarydetails = [NSJSONSerialization
                                           JSONObjectWithData:reponsedata
                                           options:kNilOptions
                                           error:nil];
        NSLog(@"The return data is: %@",Dictionarydetails);

        NSMutableDictionary *tempdict = [Dictionarydetails valueForKey:@"AppConfig"];
        array=[tempdict valueForKey:@"RestInfo"];

        NSLog(@"the result  are %@",array);
        NSLog(@"the result count is are %d",[array count]);


        NSDictionary *classDict  = [[NSDictionary alloc]init];

        for (int i=0; i<[array count]; i++) {
            //arr = [Class_location objectForKey:@"class_image"];
            classDict =[array objectAtIndex:i];
            // NSLog(@"the  dict Values are  are: %@",classDict);
            NSMutableArray  *dict1 =[[NSMutableArray alloc]init];
            dict1 =[classDict valueForKey:@"RestInfo"];
            NSLog(@"the result :%@",dict1);

            lb1.text =[classDict valueForKey:@"restaurant_location"];
            lbl2.text = [classDict valueForKey:@"restaurant_name"];
            lbl3.text = [classDict valueForKey:@"contact_name"];

        }
    }
}

问题是我无法获得这些价值标签 好吧,我收到了来自服务器的回复

4 个答案:

答案 0 :(得分:4)

//your answer for the script this code check the ans in console, i am waiting for your response


- (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:@"RestInfo"];

for (int i=0; i<[array count]; i++) {
    NSLog(@"the restrunt==%@",[[array objectAtIndex:i]objectForKey:@"restaurant_location"]);
    NSLog(@"the resname==%@",[[array objectAtIndex:i]objectForKey:@"restaurant_name"]);
    NSLog(@"the resname==%@",[[array objectAtIndex:i]objectForKey:@"contact_name"]);

}

}

<强>夫特

override func viewDidLoad() {
 super.viewDidLoad()
 var request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://advantixcrm.com/prj/mitech/index.php/api/appconfig/Mg")!)
 request.HTTPMethod = "GET"
request.setValue("application/json;charset=UTF-8", forHTTPHeaderField: "content-type")
NSError * err
NSURLResponse * response
var responseData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: response!, error: err!)
var jsonArray: [NSObject : AnyObject] = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingMutableContainers, error: err!)
var array: [AnyObject] = (jsonArray["RestInfo"] as! [AnyObject])
for var i = 0; i < array.count; i++ {
    NSLog("the restrunt==%@", (array[i]["restaurant_location"] as! String))
    NSLog("the resname==%@", (array[i]["restaurant_name"] as! String))
    NSLog("the resname==%@", (array[i]["contact_name"] as! String))
 }
}

答案 1 :(得分:1)

使用此:

NSError *err= nil;
NSArray* arrayDetails= [NSJSONSerialization
                                   JSONObjectWithData:reponsedata
                                   options:kNilOptions
                                   error:&err];
[arrayDetails enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
    if([obj objectForKey:@"event_date"] isEqualTo:@"myDate")
    {
         lb1.text =[obj objectForKey:@"restaurant_location"];
            lbl2.text = [obj objectForKey:@"restaurant_name"];
            lbl3.text = [obj objectForKey:@"contact_name"];
    }

}];

答案 2 :(得分:0)

    NSArray *arrayDetails = [NSJSONSerialization
                                   JSONObjectWithData:reponsedata
                                   options:kNilOptions
                                   error:nil];
    for(NSDictionary* dict in arrayDetails) {
     lb1.text  = dict[@"RestInfo"][@"restaurant_location"];
     lbl2.text = dict[@"RestInfo"][@"restaurant_name"];
     lbl3.text = dict[@"RestInfo"][@"contact_name"];
    }

我认为这是必要的

答案 3 :(得分:0)

如果你仔细观察你的json反应,你会发现以下结构,

enter image description here

词典 - &gt; RestInfo 数组 - &gt; 必需词典

所以你的解析器代码应该如下,

NSDictionary *rootDictionary = [NSJSONSerialization JSONObjectWithData:reponsedata
                                                   options:kNilOptions
                                                   error:nil];

NSArray *restInfoArray = [rootDictionary valueForKey:@"RestInfo"];
for (NSDictionary *dict in rootDictionary) {
    lbl1.text = [dict valueForKey:@"restaurant_name"];
    lbl2.text = [dict valueForKey:@"restaurant_location"];
    lbl3.text = [dict valueForKey:@"contact_name"];
}

请注意,如果RestInfo数组返回多个词典,lbl1lbl2lbl3将显示最后一个词典键的文本。您需要具有适当的UI来处理此类方案。

希望有所帮助!