[已解决]php如何解析这样的json字符串
var xx = 'dfdf';
var yy = 'kfdl';
var zz = 'ijklk';
instance:[{"instance_name":xx,"machine_belong":yy,"access_port":zz},{"instance_name":xx,"machine_belong":yy,"access_port":zz}]
php后台如何处理
instance = [{"instance_name":1,"machine_belong":2,"access_port":3},{"instance_name":1,"machine_belong":2,"access_port":3}];
用这个做了测试,是json写的不对,我代码里json是拼接的,拼接有错误
回复内容:
var xx = 'dfdf';
var yy = 'kfdl';
var zz = 'ijklk';
instance:[{"instance_name":xx,"machine_belong":yy,"access_port":zz},{"instance_name":xx,"machine_belong":yy,"access_port":zz}]
php后台如何处理
instance = [{"instance_name":1,"machine_belong":2,"access_port":3},{"instance_name":1,"machine_belong":2,"access_port":3}];
用这个做了测试,是json写的不对,我代码里json是拼接的,拼接有错误
instance = [{"instance_name":xx,"machine_belong":yy,"access_port":zz},{"instance_name":xx,"machine_belong":yy,"access_port":zz}]
// 对instance json序列化, 才可以进行数据传输
instance = JSON.stringify(instance);
PHP解析:
$instance = json_decode($_REQUEST['instance'], true);
不谢
你应该把对象先转成 字串符 ,然后再发送给后台
看到你的代码,是js发往php接口?
如果是这样,用post,php可以用$res=$_post['data']
假如你的调用接口是以,ajax({data:{...}});
接下来,php的获取对象,类似res.name
https://github.com/yiisoft/yi...
请参考这个代码
http://json.cn/ 测试json格式
上一篇: 环信服务端集成(PHP)