什么';在iOS应用程序中使用JSON的最佳方式是什么?

什么';在iOS应用程序中使用JSON的最佳方式是什么?,ios,objective-c,json,Ios,Objective C,Json,我最近一直在研究iOS平台的网络方面。我想将JSON提要合并到我的应用程序中。因此,我有几个问题: 解析JSON对象并检索它们的最佳内置类是什么 下载JSON数据时,如何存储和使用它 我是否使用NSURL和NSURLRequest 我是iOS开发新手,请帮助我。谢谢。对于我应用程序中的JSON帖子,我使用SBJson。此方法使用NSURLConnection和HTTP Post请求。解析后的返回数据将以键和字符串的形式出现在NSDictionary中。(jsonData是在elese的某个地方建

我最近一直在研究iOS平台的网络方面。我想将JSON提要合并到我的应用程序中。因此,我有几个问题:

  • 解析JSON对象并检索它们的最佳内置类是什么
  • 下载JSON数据时,如何存储和使用它
  • 我是否使用
    NSURL
    NSURLRequest


    我是iOS开发新手,请帮助我。谢谢。

    对于我应用程序中的JSON帖子,我使用SBJson。此方法使用NSURLConnection和HTTP Post请求。解析后的返回数据将以键和字符串的形式出现在NSDictionary中。(jsonData是在elese的某个地方建立的NSJ字典)(显然NSJSONSerialization也做了同样的事情)


    对于我应用程序中的JSON帖子,我使用SBJson。此方法使用NSURLConnection和HTTP Post请求。解析后的返回数据将以键和字符串的形式出现在NSDictionary中。(jsonData是在elese的某个地方建立的NSJ字典)(显然NSJSONSerialization也做了同样的事情)


    序列化json的内置方法是

    用作:

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //  json data
    
    反序列化为:

    NSArray *arrayOfJson = [NSJSONSerialization JSONObjectWithData:response
    options:0 error:&jsonParsingError];
    
    但是应该知道json返回的是什么格式。可以是NSArray或NSDictionary

    从JSON获取数据

    for(int i=0; i<[arrayOfJson count];i++)
    {
        arr= [arrayOfJson objectAtIndex:i];
        NSLog(@”Json value: %@”, [arr objectForKey:@"youJSONKey"]);
    }
    

    for(inti=0;i序列化json的内置方法是

    用作:

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //  json data
    
    反序列化为:

    NSArray *arrayOfJson = [NSJSONSerialization JSONObjectWithData:response
    options:0 error:&jsonParsingError];
    
    但应该知道json返回的是什么格式,可以是NSArray或NSDictionary

    从JSON获取数据

    for(int i=0; i<[arrayOfJson count];i++)
    {
        arr= [arrayOfJson objectAtIndex:i];
        NSLog(@”Json value: %@”, [arr objectForKey:@"youJSONKey"]);
    }
    

    for(int i=0;i检索JSON数据的最佳方法取决于您的用例,因此,我无法帮助您

    在iOS中解析JSON非常简单。您只需使用类将JSON数据转换为NSArray或NSDictionary(取决于JSON数据的根元素)

    从web服务下载和解析JSON(以数组作为基本对象)的示例:

    JSON的格式应该类似于本例中的
    [“1”、“2”、“last”]

    在.h中:

    @interface WSHandler : NSObject <NSURLConnectionDelegate>
    {
        NSMutableData *receivedData;
    }
    
    - (void)callWebService;
    @end
    
    @接口WSHandler:NSObject
    {
    NSMutableData*接收数据;
    }
    -(作废)callWebService;
    @结束
    
    在.m中:

    #import "WSHandler.h"
    
    @implementation WSHandler
    - (void)callWebService
    {   
    
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:/* here goes your WS URL*/]];
    
        [request setHTTPMethod:@"GET"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
        if (connection)
        {
            receivedData = nil;
        }
        else
        {
            NSLog(@"Connection could not be established");
        }
    }
    
    #pragma mark - NSURLConnection delegate methods
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [receivedData setLength:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        if (!receivedData)
            receivedData = [[NSMutableData alloc] initWithData:data];
        else
            [receivedData appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        NSLog(@"***** Connection failed");
    
        receivedData=nil;
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        // do something with the data
        NSLog(@"***** Succeeded! Received %d bytes of data",[receivedData length]);
        NSLog(@"***** AS UTF8:%@",[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]);
    
        //JSON parsing
        NSError *err = nil;
        NSArray *receivedDataArray = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&err];
    
        if (receivedDataArray==nil || receivedDataArray.count<=0 || err)
        {
            DLog(@"***** Error! %@", [err localizedDescription]);
        }
        else
        {
            //logs each element of the array
            for (int i=0;i<receivedDataArray.count;i++)
            {
                NSLog(@"Element %i = %@",i,[receivedDataArray objectAtIndex:i]);
            }       
        }    
    }
    @end
    
    #导入“WSHandler.h”
    @实现WSHandler
    -(void)callWebService
    {   
    NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:/*这是您的WS-URL*/];
    [请求设置HttpMethod:@“获取”];
    [请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
    NSURLConnection*connection=[[NSURLConnection alloc]initWithRequest:request委托:self];
    如果(连接)
    {
    接收数据=零;
    }
    其他的
    {
    NSLog(@“无法建立连接”);
    }
    }
    #pragma标记-NSURLConnection委托方法
    -(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
    {
    [接收数据集长度:0];
    }
    -(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
    {
    如果(!receivedData)
    receivedData=[[NSMutableData alloc]initWithData:data];
    其他的
    [接收数据附录数据:数据];
    }
    -(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
    {
    NSLog(@“******连接失败”);
    接收数据=零;
    }
    -(无效)连接IDFinishLoading:(NSURLConnection*)连接
    {
    //对数据做点什么
    NSLog(@******成功!接收到%d字节的数据,[receivedData length]);
    NSLog(@“****作为UTF8:%@,[[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding]);
    //JSON解析
    n错误*err=nil;
    NSArray*receivedDataArray=[NSJSONSerialization JSONObjectWithData:receivedData选项:NSJSONReadingMutableContainers错误:&err];
    
    如果(receivedDataArray==nil | | receivedDataArray.count检索JSON数据的最佳方式取决于您的用例,因此,我无法帮助您

    在iOS中解析JSON非常简单。您只需使用类将JSON数据转换为NSArray或NSDictionary(取决于JSON数据的根元素)

    从web服务下载和解析JSON(以数组作为基本对象)的示例:

    JSON的格式应该类似于本例中的
    [“1”、“2”、“last”]

    在.h中:

    @interface WSHandler : NSObject <NSURLConnectionDelegate>
    {
        NSMutableData *receivedData;
    }
    
    - (void)callWebService;
    @end
    
    @接口WSHandler:NSObject
    {
    NSMutableData*接收数据;
    }
    -(作废)callWebService;
    @结束
    
    在.m中:

    #import "WSHandler.h"
    
    @implementation WSHandler
    - (void)callWebService
    {   
    
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:/* here goes your WS URL*/]];
    
        [request setHTTPMethod:@"GET"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
        if (connection)
        {
            receivedData = nil;
        }
        else
        {
            NSLog(@"Connection could not be established");
        }
    }
    
    #pragma mark - NSURLConnection delegate methods
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [receivedData setLength:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        if (!receivedData)
            receivedData = [[NSMutableData alloc] initWithData:data];
        else
            [receivedData appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        NSLog(@"***** Connection failed");
    
        receivedData=nil;
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        // do something with the data
        NSLog(@"***** Succeeded! Received %d bytes of data",[receivedData length]);
        NSLog(@"***** AS UTF8:%@",[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]);
    
        //JSON parsing
        NSError *err = nil;
        NSArray *receivedDataArray = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&err];
    
        if (receivedDataArray==nil || receivedDataArray.count<=0 || err)
        {
            DLog(@"***** Error! %@", [err localizedDescription]);
        }
        else
        {
            //logs each element of the array
            for (int i=0;i<receivedDataArray.count;i++)
            {
                NSLog(@"Element %i = %@",i,[receivedDataArray objectAtIndex:i]);
            }       
        }    
    }
    @end
    
    #导入“WSHandler.h”
    @实现WSHandler
    -(void)callWebService
    {   
    NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:/*这是您的WS-URL*/];
    [请求设置HttpMethod:@“获取”];
    [请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
    NSURLConnection*connection=[[NSURLConnection alloc]initWithRequest:request委托:self];
    如果(连接)
    {
    接收数据=零;
    }
    其他的
    {
    NSLog(@“无法建立连接”);
    }
    }
    #pragma标记-NSURLConnection委托方法
    -(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
    {
    [接收数据集长度:0];
    }
    -(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
    {
    如果(!receivedData)
    receivedData=[[NSMutableData alloc]initWithData:data];
    其他的
    [接收数据附录数据:数据];
    }
    -(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
    {
    NSLog(@“******连接失败”);
    接收数据=零;
    }
    -(无效)连接IDFinishLoading:(NSURLConnection*)连接
    {
    //对数据做点什么
    NSLog(@******成功!接收到%d字节的数据,[receivedData length]);
    NSLog(@“****作为UTF8:%@,[[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding]);
    //JSON解析
    NSErr