要修改JSON中的特定键值对,可以按照以下步骤进行操作:
json_decode()
函数来实现:$json = '{"name":"John", "age":30, "city":"New York"}';
$arr = json_decode($json, true);
$arr['name'] = 'Tom';
$arr['age'] = 25;
json_encode()
函数来实现:$newJson = json_encode($arr);
完整的代码示例:
$json = '{"name":"John", "age":30, "city":"New York"}';
$arr = json_decode($json, true);
$arr['name'] = 'Tom';
$arr['age'] = 25;
$newJson = json_encode($arr);
echo $newJson;
输出结果:
{"name":"Tom","age":25,"city":"New York"}
通过这种方式,你可以轻松地修改JSON中的指定键值对。