在PHP中,可以使用json_encode()和json_decode()函数对JSON数据进行序列化和反序列化。
$data = array( "name" => "John", "age" => 30, "city" => "New York");// 使用json_encode()函数将数组转换为JSON字符串$json_string = json_encode($data);echo $json_string; // 输出:{"name":"John","age":30,"city":"New York"}反序列化(将JSON字符串转换为数组或对象):$json_string = '{"name":"John","age":30,"city":"New York"}';// 使用json_decode()函数将JSON字符串转换为数组$array = json_decode($json_string, true);print_r($array); // 输出:Array ( [name] => John [age] => 30 [city] => New York )// 使用json_decode()函数将JSON字符串转换为对象$object = json_decode($json_string);echo $object->name; // 输出:John注意:在使用json_decode()函数时,第二个参数设置为true表示将JSON字符串转换为关联数组;如果不设置或设置为false,则将JSON字符串转换为对象。


