kok*_*oko 2 iphone xcode json get ios
我试图解析来自Web服务器的响应数据.我正在使用Xcode 4.2.
NSLog(@"Establishing connection...");
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"example.com/test.php"]]; [request setHTTPMethod:@"GET"];
accept = @"Kokokoko";
NSLog(@"Adding value for HTTP Header");
[request addValue:accept forHTTPHeaderField: @"Accept"];
NSLog(@"Sending request...");
//send request & get response
returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Request sent");
Run Code Online (Sandbox Code Playgroud)
然后我想使用returnData来解析JSON响应.它应该是
["Test", "42", "OK"]
Run Code Online (Sandbox Code Playgroud)
所以我试图使用NSDictionary和JSONValue等技巧,但我没有成功.也许这个问题有一个简单的解决方案?
如果您希望服务器返回如下字符串:
["Test", "42", "OK"]
Run Code Online (Sandbox Code Playgroud)
然后它的JSONValue将是NSArray(不是NSDictionary).试试这个:
NSString* responseString = [[[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding] autorelease];
NSLog(@"Response String: %@", responseString);
// Supposing that you are using SBJson, the parsing part may look like this:
//
NSArray* responseArray = [returnString JSONValue];
NSLog(@"Response Array: %@", responseArray);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1535 次 |
| 最近记录: |