저는 iPhone에 익숙하지 않습니다. NSMutable 배열을 json 문자열로 변환하려고합니다. 요청을 사용하여이 문자열을 PHP 파일로 보내고 NSLog에 응답하여 다시 인쇄하십시오. 전송되었습니다. 그래서 내가있는 viewDidLoadASIFormDataRequest를 사용하여 PHP를 json으로 보냅니다.
NSString *phpUrl = @"http://dt-works.com/eman/bookMarks.php";
NSMutableArray *arrayKey = [[NSMutableArray alloc] initWithObjects:@"one", @"two", @"three", nil];
NSMutableArray *arrayValue1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
NSMutableArray *arrayValue2 = [[NSMutableArray alloc] initWithObjects:@"11", @"22", @"33", nil];
NSDictionary *theReqDictionary1 = [NSDictionary dictionaryWithObjects:arrayValue1 forKeys:arrayKey];
NSDictionary *theReqDictionary2 = [NSDictionary dictionaryWithObjects:arrayValue2 forKeys:arrayKey];
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:theReqDictionary1,theReqDictionary2, nil];
NSString *jsonString = [myArray JSONRepresentation];
NSLog(@"%@", jsonString);
NSDictionary *questions = nil;
NSURL *link = [NSURL URLWithString:[phpUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request setPostValue:jsonString forKey:@"jsonString"];
[request setTimeOutSeconds:120];
[request setDelegate:self];
NSError *error = [request2 error];
[request2 startSynchronous];
if (!error) {
NSData *response = [request responseData];
NSString *json = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
questions = [json objectFromJSONString];
NSLog(@"Data: %@", questions);
}
에 다음 코드를 쓴 bookMarks.php의 코드는 다음과 같습니다
$handle = fopen('php://input','r');
$jsonInput = $_POST['jsonString'];
print_r($jsonInput);
하지만 나 제공합니다
[{"one":"1","two":"2","three":"3"},{"one":"11","two":"22","three":"33"}]
Data: (null)
을 나는 데이터로되고 싶어 : "1", "2": "2", "3": "3"}, { "하나": "11", "2": "22", " 33 "}] 이 작업을 수행하는 방법 ??
당신은 당신이 JSON을 보낼 필요가시겠습니까? PHP 폼에 이르면 모든 가상 변수를 POST 또는 GET으로 쉽게 보낼 수 있습니다. –
예 NSMUtableArray 변수를 json으로 보낼 필요가 있습니다. 변수를 사용하기 전에 변수를 시도했는데 작동합니다. 이번에는 json으로 배열을 보내려고합니다. –