php如何得到josn有几组数组_php – 如何将两个具有不同类型实体的数组合并到一个json对象中?...

您可以使用两个数组,然后使用两个键(人员和汽车)构建一个新字典,使用两个数组作为与这两个键关联的值.例如:

NSArray *people = @[@{@"index":@"1",@"lastName":@"Brown",@"firstName":@"Kathy",@"company":@"ABC inc."},

@{@"index":@"2",@"lastName":@"Smith",@"firstName":@"Mike",@"company":@"XYZ inc."}];

NSArray *cars = @[@{@"index":@"1",@"make":@"Toyota",@"model":@"RAV4",@"year":@"2009"},

@{@"index":@"2",@"make":@"Honda",@"model":@"Pilot",@"year":@"2012"}];

NSDictionary *fullDictionary = @{@"people": people, @"cars": cars};

NSError *error;

NSData *data = [NSJSONSerialization dataWithJSONObject:fullDictionary options:0 error:&error];

生成的JSON(格式化,以便您可以更轻松地阅读)看起来像:

{

"cars" : [

{

"model" : "RAV4",

"year" : "2009",

"make" : "Toyota",

"index" : "1"

},

{

"model" : "Pilot",

"year" : "2012",

"make" : "Honda",

"index" : "2"

}

],

"people" : [

{

"lastName" : "Brown",

"firstName" : "Kathy",

"company" : "ABC inc.",

"index" : "1"

},

{

"lastName" : "Smith",

"firstName" : "Mike",

"company" : "XYZ inc.",

"index" : "2"

}

]

}

要发送该请求,您可以执行以下操作:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

request.HTTPBody = data;

request.HTTPMethod = @"POST";

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

// check for fundamental networking error (e.g. not connected to Internet)

if (error) {

NSLog(@"error = %@", error);

return;

}

// also check to see if the server reported some HTTP error

if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

if (statusCode != 200) {

NSLog(@"statusCode = %ld", (long)statusCode);

return;

}

}

// OK, if we've gotten here, let's parse the response; I'm assuming you'll send JSON response; so parse it

NSError *parseError;

NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

if (parseError) {

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"responseString = %@", responseString);

} else {

NSLog(@"responseObject = %@", responseObject);

}

}];

[task resume];

在PHP中,要解析此响应,您可以执行以下操作:

header("Content-Type: application/json");

$handle = fopen("php://input", "rb");

$raw_post_data = '';

while (!feof($handle)) {

$raw_post_data .= fread($handle, 8192);

}

fclose($handle);

$json_data = json_decode($raw_post_data, true);

$people = $json_data["people"];

$cars = $json_data["cars"];

foreach ($people as $person) {

$index = $person["index"];

$last_name = $person["lastName"];

$first_name = $person["firstName"];

$company = $person["company"];

// use these four variables to insert row of data here; note, make sure you either use

// `mysqli::real_escape_string` or that you manually bind these values to `?` placeholders

}

foreach ($cars as $car) {

$index = $car["index"];

$make = $car["make"];

$model = $car["model"];

$year = $car["year"];

// again, use these four variables to insert row of data here; note, make sure you either use

// `mysqli::real_escape_string` or that you manually bind these values to `?` placeholders

}

// obviously, if we had errors above, we'd send something like Array("success" => false, ...) with error messages and error codes

$response = Array("success" => true);

echo json_encode($response);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值