要修改JSON中的特定键值对,可以按照以下步骤进行操作:
首先,将JSON数据解码为PHP数组,可以使用json_decode() 函数来实现:$json = '{"name":"John", "age":30, "city":"New York"}';$arr = json_decode($json, true);接下来,使用数组操作来修改指定的键值,例如:$arr['name'] = 'Tom';$arr['age'] = 25;最后,将修改后的数组重新编码为JSON,可以使用 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中的指定键值对。

