ios - 解析JSON数据以进行标记

标签 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"];

        }
    }
}

问题是我无法获得这些值的标签
好吧,我得到了服务器的回应

最佳答案

//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))
 }
}

关于ios - 解析JSON数据以进行标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21449848/

相关文章:

iphone - iOS 持久通知/快速启动

ios - iOS 上的 OpenCV - GPU 使用情况?

php - 从 APNS 接收推送通知时自动增加角标(Badge)数

ios - 如何快速减少表格 View 中单元格的 swipeAction 距离? (如 WhatsApp 上下文回复功能)

objective-c - 如何将 JSON 对象映射到 Objective C 类?

ios - Swift Bridging Header - 使用未声明的类型 'FMDatabase' 错误

ios - 如何在iOS中用MPMusicPlayerController的applicationMusicPlayer播放背景音乐?

ios - iOS "Failed to start recording using AvAudioRecorder"上的 Phonegap 3.5 媒体插件错误

iphone - 更新 iOS 7 map 相机旋转的 map 注释

ios - Autolayout 动画约束不动画 subview