【问题标题】:how to handle the responseString from json output and parse it如何处理来自json输出的responseString并解析它
【发布时间】:2011-06-15 06:55:53
【问题描述】:

我正在使用以下代码,并且我得到了 JSON 格式的响应字符串。我不知道如何解析它。如果我想在 JSON 输出上调用任何方法,下一步该怎么做

//源代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    dataWebService = [[NSMutableData data] retain];

    NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.googleapis.com/customsearch/v1?key=AIzaSyDzl0Ozijg2C47iYfKgBWWkAbZE_wCJ-2U&cx=017576662512468239146:omuauf_lfve&q=lectures&callback=handleResponse"]]retain];    

    NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];

    [myConnection start];    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    [dataWebService setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [dataWebService appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
    NSLog(@"Response: %@",responseString);

    [responseString release];
    [dataWebService release];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Error during connection: %@", [error description]);
}

【问题讨论】:

    标签: iphone objective-c ios json


    【解决方案1】:

    使用 JSON 解析框架:http://code.google.com/p/json-framework/

    【讨论】:

    • 感谢您的回复,我得到了输出,但是如何按照以下网站iphonedevsdk.com/forum/iphone-sdk-tutorials/…中提供的教程进行解析@
    • 是的,就像这样: NSDictionary *dictionary = [responseString JSONValue];没有变得更容易
    • 我在哪个方法中调用 NSDictionary *dictionary?我是 iphone 新手,我不太懂编码..
    • 代替:NSLog(@"Response: %@",responseString);
    猜你喜欢
    • 2015-08-05
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2021-09-09
    相关资源
    最近更新 更多