我试图将 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 字符串而变化。有人可以在这方面帮助我吗?