普通网友
2016-06-01 16:18
浏览 167
已采纳

json_decode返回带有效json的语法错误

I'm trying to decode the following json which I have checked with jsonlint and returns that it is a valid json:

[{"gatewayId": "5154979870408704", "sensorId": "5698497110081536"}, {"timestamp": "20160513T225959000Z", "tagId": "123456789AB1", "io": "in"}, {"timestamp": "20160513T235959000Z", "tagId": "123456789AB2", "io": "out"}]

However the result is always Syntax Error when I call json_last_error_msg() Here is my code:

$param = substr($HTTP_RAW_POST_DATA, 1);
$param =utf8_encode($param);
$json = json_decode($param,true);
echo json_last_error_msg();
var_dump($param);

The json is being sent to me using curl which is why it is in $HTTP_RAW_POST_DATA, and I'm removing the first character because when making a var_dump I noticed a blank character there, however that's not the problem. Any help is appreciated. EDIT: This is the result of var_dump($HTTP_RAW_POST_DATA)

string(187) " [{gatewayId: 5154979870408704, sensorId: 5698497110081536}, {timestamp: 20160513T225959000Z, tagId: 123456789AB1, io: in}, {timestamp: 20160513T235959000Z, tagId: 123456789AB2, io: out}]"

EDIT2: So turns out it was because the double quotes were being removed in the curl request that had the payload like

-d "[{"gatewayId": "5154979870408704", "sensorId": "5698497110081536"}, {"timestamp": "20160513T225959000Z", "tagId": "123456789AB1", "io": "in"}, {"timestamp": "20160513T235959000Z", "tagId": "123456789AB2", "io": "out"}]"

I replaced the double quotes with single quotes in the begining and end of the payload and now its working fine, just leaving this here in case anyone has the same problem. new payload:

-d '[{"gatewayId": "5154979870408704", "sensorId": "5698497110081536"}, {"timestamp": "20160513T225959000Z", "tagId": "123456789AB1", "io": "in"}, {"timestamp": "20160513T235959000Z", "tagId": "123456789AB2", "io": "out"}]'

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试解码我​​用jsonlint检查的以下json并返回它是一个有效的json:

[{“gatewayId”:“5154979870408704”,“sensorId”:“5698497110081536”},{“timestamp”:“20160513T225959000Z”,“tagId”:“123456789AB1 “,”“io”:“in”},{“timestamp”:“20160513T235959000Z”,“tagId”:“123456789AB2”,“io”:“out”}] \ n

但是当我调用json_last_error_msg()时,结果始终是语法错误 这是我的代码:

  $ param = substr($ HTTP_RAW_POST_DATA,1); 
  $ param = utf8_encode($ param); 
 $ json = json_decode($ param,true); 
echo json_last_error_msg(); 
var_dump($ param); 
   
 
 <  p> json正在使用curl发送给我,这就是为什么它在$ HTTP_RAW_POST_DATA中,并且我删除了第一个字符,因为在创建var_dump时我发现了一个空白字符,但这不是问题。 感谢任何帮助。
 编辑: 
这是var_dump($ HTTP_RAW_POST_DATA)的结果 
 
 

string(187)“[{ gatewayId:5154979870408704,sensorId:5698497110081536},{timestamp:20160513T225959000Z,tagId:123456789AB1,io:in},{timestamp:20160513T235959000Z,tagId:123456789AB2,io:out}]“

EDIT2: 原来这是因为在带有有效负载的curl请求中删除了双引号

   -d“[{”gatewayId“:”5154979870408704“,”sensorId“:”5698497110081536“},{”timestamp“:”20160513T225959000Z“,”tagId“:”123456789AB1“,”io“:”in“},{”  timestamp“:”20160513T235959000Z“,”tagId“:”123456789AB2“,”io“:”out“}]”
   
 
 

我用单引号替换双引号 在有效载荷的开始和结束时,现在它的工作正常,只要将其留在这里以防任何人有同样的问题。 新的有效载荷:

  -d'[{“  gatewayId“:”5154979870408704“,  “sensorId”:“5698497110081536”},{“timestamp”:“20160513T225959000Z”,“tagId”:“123456789AB1”,“io”:“in”},{“timestamp”:“20160513T235959000Z”,“tagId”:“123456789AB2  “,”“io”:“out”}]'
   
 
 

谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dr5648 2016-06-01 16:31
    最佳回答

    In the var_dump I read

    string(187) " [{gatewayId: 5154979870408704, ...
    

    Here the key gatewayId is not quoted. The JSON standard requires keys to be strings which use "". The data thus is not valid JSON and PHP rejects it.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题