2014-07-07 89 views
1

我是iOS開發新手。需要幫助使用NSJSONSerialization解析這些數據。數據來自post方法。 數據是這樣 [ { 「edition_id」: 「1」, 「錯誤」:假​​, 「LONG_NAME」: 「拉傑科德」, 「消息」: 「RESULT_OK」, 「SHORT_NAME」:「RJT 「 } ]我的文件使用NSJsonSerialization在iOS7中使用JSON解析提取數據

和代碼就像使用通過後JSON解析這個

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]; 

回答

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"]); 
0

獲取數據方法

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"); 
    }