【发布时间】:2014-11-07 12:43:38
【问题描述】:
这是我从 url 获取 post json 数组的代码
// SENDING A POST JSON
NSString *post = [NSString stringWithFormat:@"plm=1"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://muvieplus.com/testjson/test.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];
NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
NSLog(@"%@", requestReply);
当我运行它时,我得到了 requestReply
2014-11-07 14:22:15.565 JsonApp[1849:60b]
{
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
我如何解析这个 json?有什么帮助吗?
【问题讨论】:
-
我建议使用 JSON 解析器,例如 NSJSONSerialization,如果您在提出问题之前稍作搜索,就会发现它。