Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我们如何使用JSON查询从服务器获取数据?_Ios_Json - Fatal编程技术网

Ios 我们如何使用JSON查询从服务器获取数据?

Ios 我们如何使用JSON查询从服务器获取数据?,ios,json,Ios,Json,**在这段代码中,当从viewdidload()函数添加断点时,我得到了greetingArray.count零,但当我在for循环中添加断点时,它工作正常,结果3作为greetingArray的值。无法从服务器获取数据的可能原因是什么。服务器端没有问题。我已经检查过了 - (void)viewDidLoad { [super viewDidLoad]; greetingArray = [[NSMutableArray alloc] init]; greetingDict

**在这段代码中,当从viewdidload()函数添加断点时,我得到了greetingArray.count零,但当我在for循环中添加断点时,它工作正常,结果3作为greetingArray的值。无法从服务器获取数据的可能原因是什么。服务器端没有问题。我已经检查过了

- (void)viewDidLoad
{
    [super viewDidLoad];
    greetingArray = [[NSMutableArray alloc] init];
    greetingDictionary = [[NSMutableDictionary alloc] init];
         NSString *connectionString;
    connectionString=[NSString stringWithFormat:@"http://xxx.xxx.x.xx/TestMgt/api/%@",self.fieldName];

    NSURL *url = [NSURL URLWithString:connectionString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data, NSError *connectionError)
     {
         if (data.length > 0 && connectionError == nil)
         {
             NSLog(@"----------------------------------------------------");
             NSLog(@"Data length is = %d",data.length);
            greetingMArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
             NSLog(@"%@",greetingMArray);
             for(int i = 0 ; i< greetingMArray.count; i++)
             {
                 greetingDictionary = (NSMutableDictionary *)[greetingMArray objectAtIndex:i];
                 NSLog(@"%@",greetingDictionary);
                 ConnectionOvertime *overtime = [[ConnectionOvertime alloc] init];
                 overtime.entryDate=[greetingDictionary valueForKey:@"EntryDate"];
                 [greetingArray addObject:overtime];
                 NSLog(@"%d",greetingArray.count);
             }      
         }
     }];
}
-(void)viewDidLoad
{
[超级视图下载];
greetingArray=[[NSMutableArray alloc]init];
greetingDictionary=[[NSMutableDictionary alloc]init];
NSString*连接字符串;
connectionString=[NSString stringWithFormat:@”http://xxx.xxx.x.xx/TestMgt/api/%@“,self.fieldName];
NSURL*url=[NSURL URLWithString:connectionString];
NSURLRequest*request=[nsurlRequestRequestWithURL:url];
[NSURLConnection sendAsynchronousRequest:请求
队列:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse*响应,
NSData*数据,NSError*连接错误)
{
如果(data.length>0&&connectionError==nil)
{
NSLog(@“------------------------------------------------------------”);
NSLog(@“数据长度=%d”,Data.length);
greetingMArray=[NSJSONSerialization JSONObjectWithData:数据选项:0错误:NULL];
NSLog(@“%@”,问候Marray);
for(int i=0;i
如果您没有得到任何答案,请尝试jsonFramework库并导入sbjsonParser.h
例如,请尝试下面的代码
-(无效)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
self.ChapterID=[[NSMutableArray alloc]init];
self.ChapterName=[[NSMutableArray alloc]init];
NSURL*url=[NSURL URLWithString:@”https://www.coursekart.com/webservice/load-subjects.php?api_key=68410920GHJFLAC878&standard_id=2&format=json"];
NSURLRequest*request=[[NSURLRequest alloc]initWithURL:url];
n错误*错误;
NSURLResponse*响应;
NSData*data=[NSURLConnection sendSynchronousRequest:请求返回应答:&响应错误:&错误];
如果(数据!=nil)
{
NSString*content=[[NSString alloc]initWithData:数据编码:NSUTF8StringEncoding];
if(content.length!=0)
{
SBJsonParser*parser=[[SBJsonParser alloc]init];
NSArray*dir=[[parser objectWithString:content]objectForKey:@“SubjectList”];

对于(int i=0;我知道
sendAsynchronousRequest
在做什么吗?
     if you don't get any answer try jsonFramework library and import sbjsonParser.h 


for Example try below code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.ChapterID=[[NSMutableArray alloc]init];
self.ChapterName=[[NSMutableArray alloc]init];
NSURL *url=[NSURL URLWithString:@"https://www.coursekart.com/webservice/load-subjects.php?api_key=68410920GHJFLAC878&standard_id=2&format=json"];
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
NSError *error;
NSURLResponse *response;

NSData *data=[NSURLConnection  sendSynchronousRequest:request returningResponse:&response error:&error];
if(data!=nil)
{
    NSString *content=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if(content.length!=0)
    {
        SBJsonParser *parser=[[SBJsonParser alloc]init];
        NSArray *dir=[[parser objectWithString:content]objectForKey:@"SubjectList"];


        for(int i=0;i<dir.count;i++)
        {
            NSDictionary *array=[dir objectAtIndex:i];
            NSArray *data=[array objectForKey:@"Data"];
            NSDictionary *dat=(NSDictionary *)data;
            NSString *idCh=[dat objectForKey:@"id"];
            NSString *slug=[dat objectForKey:@"slug"];

            [ChapterID addObject:idCh];
            [ChapterName addObject:slug];
           // NSLog(@"%@",[ChapterID objectAtIndex:0]);
            //NSLog(@"%@",[ChapterName objectAtIndex:0]);

        }
    }
   }
}