使用NSJsonSerialization通过iOS7中的post方法使用JSON解析来获取数据

时间:2014-07-07 08:25:20

标签: json parsing post ios7

我是iOS开发的新手。需要帮助来使用NSJSONSerialization解析此数据。数据来自post方法。 数据是这样的 [     {         “edition_id”:“1”,         “错误”:错误,         “long_name”:“Rajkot”,         “message”:“RESULT_OK”,         “short_name”:“RJT”     } ]

我的文件的代码就像这样

NSURL *theURL = [NSURL URLWithString:@"MYURL"];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setURL:theURL];
[theRequest setHTTPMethod:@"POST"];
NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theRequest]];
NSError *error;
NSMutableDictionary *allCourses = [NSJSONSerialization JSONObjectWithData:allCoursesData options:kNilOptions error:&error];

2 个答案:

答案 0 :(得分:0)

NSString * uid = @"2"; // variable value
NSString *post = [NSString stringWithFormat:@"uid=%@",uid]; // post variable
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://localhost/my/json_post/index.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
/* NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];
NSLog(@"json Reply : %@", theReply);
*/

NSArray *arr = [NSJSONSerialization JSONObjectWithData:POSTReply options:0 error:nil];
NSLog(@"%@",arr);

NSLog(@"%@",[arr valueForKey:@"id"]); //this is table column name
NSLog(@"%@",[arr valueForKey:@"name"]);
NSLog(@"%@",[arr valueForKey:@"surname"]);

答案 1 :(得分:0)

通过post方法

使用JSON解析获取数据
    NSHTTPURLResponse *response = nil;
    NSError *error=nil;
    NSString *post = [NSString stringWithFormat:@"Key=%@",@"value"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:@"Your URL"]];
    [request setHTTPMethod:@"POST"];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSData *conn1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"%@",conn1);

    NSString *responseString = [[NSString alloc] initWithData:conn1 encoding:NSUTF8StringEncoding];
    NSLog(@"here %@",responseString);
    if(conn) {
        NSLog(@"Connection Successful");
    } else {
        NSLog(@"Connection could not be made");
    }