使用json发送对象

dan*_*car 5 php json

是否有推荐的方法在服务器和客户端之间使用json发送对象?我应该只使用小写的属性,我应该使用对象还是数组?

更新:这个问题来找我,因为默认情况下php将关联数组编码为对象(解码后)

Roc*_*mat 5

你应该创建一个数组,然后使用PHP的json_encode方法.如果值是大写或小写,则无关紧要.

$a = array(
  'Test' => 42,
  'example' => 'Testing'
);

echo json_encode($a); // {"Test":42,"example":"Testing"}
Run Code Online (Sandbox Code Playgroud)

在PHP中解码时,传递true第二个参数json_decode以将对象转换为数组.

$data = json_decode($json, true);
Run Code Online (Sandbox Code Playgroud)