我试图将JSON字符串发布到php。我创建了JSON为
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:@{@"step1":[[NSArray alloc] initWithObjects:@{@"key1":@"value1"}, @{@"key2":@"value2"}, @{@"key3":@"value3"}, nil]}];
[arr addObject:@{@"step2":[[NSArray alloc] initWithObjects:@{@"key1":@"value1"}, @{@"key2":@"value2"}, @{@"key3":@"value3"}, nil]}];
jsonString = [NSJSONSerialization JSONObjectWithData: [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil] options:NSJSONReadingMutableContainers error:nil];
NSLog(@"JSON %@",jsonString);
将json打印为
JSON (
{
step1 = (
{
key1 = value1;
},
{
key2 = value2;
},
{
key3 = value3;
}
);
},
{
step2 = (
{
key1 = value1;
},
{
key2 = value2;
},
{
key3 = value3;
}
);
}
)
我发出请求发送json字符串如下
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"2", @"userid",jsonString , @"totaldata", nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:url]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
NSLog(@"Requesting..");
[client postPath:@"" parameters:params success:^(AFHTTPRequestOperation *operation, id response){
NSLog(@"AFN response %@", operation.responseString);
NSError *error;
NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&error];
complete(responseData);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSDictionary *nsDError = [NSDictionary dictionaryWithObjectsAndKeys:error.description, @"error", nil];
failure(nsDError);
}];
}
当我从php打印时, 我得到了回应
{"json":[{"step1":[{"key1":"value1"}]},{"step1":[{"key2":"value2"}]},{"step1":[{"key3":"value3"}]},{"step2":[{"key1":"value1"}]},{"step2":[{"key2":"value2"}]},{"step2":[{"key3":"value3"}]}]}
我的PHP脚本,
$totaldata = $_POST["totaldata"];
$response = array("json"=>$totaldata);
$this->response($response,200);
在php中,我只是打印json字符串以响应json请求。 我期望json在请求和响应中是相等的,但作为响应,json随请求的json字符串而变化。 在这方面有谁可以帮助我?