• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

怎么会有错呢,简单的json读取

php 搞代码 4年前 (2022-01-23) 19次浏览 已收录 0个评论
文章目录[隐藏]

$pingluninfo = <<<EOT
{
“title”:”123″,
“url”:”http://localhost/?p=9&#8243;,
“ttime”:1401327899094,
“sourceid”:”9″,
“parentid”:”0″,
“categoryid”:””,
“ownerid”:””,
“metadata”:””,
“comments”:[
{
“cmtid”:”358″,
“ctime”:1401327899094,
“content”:”2013年8月1日18:36:29 O(∩_∩)O~”,
“replyid”:”0″,
“user”:{
“userid”:”1″,
“nickname”:”admin”,
“usericon”:””,
“userurl”:””,
“usermetadata”:{
“area”: “北京市”,
“gender”: “1”,
“kk”: “”,
“level”: 1
}
},
“ip”:”127.0.0.1″,
“useragent”:”Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0″,
“channeltype”:”1″,
“from”:””,
“spcount”:””,
“opcount”:””,
“attachment”:[
{
“type”:1,
“desc”:””,
“url”:”http://img.sohu.itc/xxxx&#8221;
}
]
}
]
}
EOT;

$pingluninfo = json_decode($pingluninfo, true);
if(!$pingluninfo) {
die(‘错误的数据格式’);
}
?>
总是输出 错误的数据格式

回复讨论(解决方案)

看着json 不对劲。
中文在json_encode 后不是这样的吧

<?php $pingluninfo = <<<EOT{    "title":"123",    "url":"http://localhost/?p=9",    "ttime":1401327899094,    "sourceid":"9",    "parentid":"0",    "categoryid":"",    "ownerid":"",    "metadata":"",    "comments":[        {            "cmtid":"358",            "ctime":1401327899094,            "content":"2013年8月1日18:36:29 O(∩_∩)O~",            "replyid":"0",            "user":{                "userid":"1",                "nickname":"admin",                "usericon":"",                "userurl":"",                "usermetadata":{                    "area": "北京市",                    "gender": "1",                    "kk": "",                    "level": 1                }            },            "ip":"127.0.0.1",            "useragent":"Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0",            "channeltype":"1",            "from":"",            "spcount":"",            "opcount":"",            "attachment":[                {                    "type":1,                    "desc":"",                    "url":"http://img.sohu.itc/xxxx"                }            ]        }    ]}EOT;$pingluninfo = json_decode($pingluninfo, true);if(!$pingluninfo) {    die('错误的数据格式');}echo "<pre class="prettyprint linenums">";print_r($pingluninfo);echo "

“;

输出:

Array(    [title] => 123    [url] => http://localhost/?p=9    [ttime] => 1401327899094    [sourceid] => 9    [parentid] => 0    [categoryid] =>     [ownerid] =>     [metadata] =>     [comments] => Array        (            [0] => Array                (                    [cmtid] => 358                    [ctime] => 1401327899094                    [content] => 2013年8月1日18:36:29 O(∩_∩)O~                    [replyid] => 0                    [user] => Array                        (                            [userid] => 1                            [nickname] => admin                            [usericon] =>                             [userurl] =>                             [usermetadata] => Array                                (                                    [area] => 北京市                                    [gender] => 1                                    [kk] =>                                     [level] => 1                                )                        )                    [ip] => 127.0.0.1                    [useragent] => Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0                    [channeltype] => 1                    [from] =>                     [spcount] =>                     [opcount] =>                     [attachment] => Array                        (                            [0] => Array                                (                                    [type] => 1                                    [desc] =>                                     [url] => http://img.sohu.itc/xxxx                                )                        )                )        ))

测试无误

请把引号补齐!
“ttime”:1401327899094, 应为 “ttime”:”1401327899094″,
其他类同

echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';$pingluninfo = <<<EOT{    "title":"123",    "url":"http://localhost/?p=9",    "ttime":1401327899094,    "sourceid":"9",    "parentid":"0",    "categoryid":"",    "ownerid":"",    "metadata":"",    "comments":[        {            "cmtid":"358",            "ctime":1401327899094,            "content":"2013年8月1日18:36:29 O(∩_∩)O~",            "replyid":"0",            "user":{                "userid":"1",                "nickname":"admin",                "usericon":"",                "userurl":"",                "usermetadata":{                    "area": "北京市",                    "gender": "1",                    "kk": "",                    "level": 1                }            },            "ip":"127.0.0.1",            "useragent":"Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0",            "channeltype":"1",            "from":"",            "spcount":"",            "opcount":"",            "attachment":[                {                    "type":1,                    "desc":"",                    "url":"http://img.sohu.itc/xxxx"                }            ]        }    ]}EOT;$pingluninfo = json_decode($pingluninfo, true);if(!$pingluninfo) {    die('错误的数据格式');}else{    print_r($pingluninfo);}

测试过,很正常。

(    [title] => 123    [url] => http://localhost/?p=9    [ttime] => 1401327899094    [sourceid] => 9    [parentid] => 0    [categoryid] =>     [ownerid] =>     [metadata] =>     [comments] => Array        (            [0] => Array                (                    [cmtid] => 358                    [ctime] => 1401327899094                    [content] => 2013年8月1日18:36:29 O(∩_∩)O~                    [replyid] => 0                    [user] => Array                        (                            [userid] => 1                            [nickname] => admin                            [usericon] =>                             [userurl] =>                             [usermetadata] => Array                                (                                    [area] => 北京市                                    [gender] => 1                                    [kk] =>                                     [level] => 1                                )                        )                    [ip] => 127.0.0.1                    [useragent] => Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0                    [channeltype] => 1                    [from] =>                     [spcount] =>                     [opcount] =>                     [attachment] => Array                        (                            [0] => Array                                (                                    [type] => 1                                    [desc] =>                                     [url] => http://img.sohu.itc/xxxx                                )                        )                )        ))

你那是新版的吧!估计 数值型 超出最大了

你那是新版的吧!估计 数值型 超出最大了

新版的?怎么讲?

我把上面说测试正常的代码复制到我那里,就是提示数据错误。
看来真是环境问题啊

我把上面说测试正常的代码复制到我那里,就是提示数据错误。
看来真是环境问题啊

echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';$pingluninfo = <<<EOT{    "title":"123",    "url":"http://localhost/?p=9",    "ttime":1401327899094,    "sourceid":"9",    "parentid":"0",    "categoryid":"",    "ownerid":"",    "metadata":"",    "comments":[        {            "cmtid":"358",            "ctime":1401327899094,            "content":"2013年8月1日18:36:29 O(∩_∩)O~",            "replyid":"0",            "user":{                "userid":"1",                "nickname":"admin",                "usericon":"",                "userurl":"",                "usermetadata":{                    "area": "北京市",                    "gender": "1",                    "kk": "",                    "level": 1                }            },            "ip":"127.0.0.1",            "useragent":"Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0",            "channeltype":"1",            "from":"",            "spcount":"",            "opcount":"",            "attachment":[                {                    "type":1,                    "desc":"",                    "url":"http://img.sohu.itc/xxxx"                }            ]        }    ]}EOT;$pingluninfo = json_decode($pingluninfo, true);if(!$pingluninfo) {    die('错误的数据格式');}else{    print_r($pingluninfo);}

测试过,很正常。

(    [title] => 123    [url] => http://localhost/?p=9    [ttime] => 1401327899094    [sourceid] => 9    [parentid] => 0    [categoryid] =>     [ownerid] =>     [metadata] =>     [comments] => Array        (            [0] => Array                (                    [cmtid] => 358                    [ctime] => 1401327899094                    [content] => 2013年8月1日18:36:29 O(∩_∩)O~                    [replyid] => 0                    [user] => Array                        (                            [userid] => 1                            [nickname] => admin                            [usericon] =>                             [userurl] =>                             [usermetadata] => Array                                (                                    [area] => 北京市                                    [gender] => 1                                    [kk] =>                                     [level] => 1                                )                        )                    [ip] => 127.0.0.1                    [useragent] => Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0                    [channeltype] => 1                    [from] =>                     [spcount] =>                     [opcount] =>                     [attachment] => Array                        (                            [0] => Array                                (                                    [type] => 1                                    [desc] =>                                     [url] => http://img.sohu.itc/xxxx                                )                        )                )        ))

我把上面说测试正常的代码复制到我那里,就是提示数据错误。
看来真是环境问题啊

<?php $pingluninfo = <<<EOT{    "title":"123",    "url":"http://localhost/?p=9",    "ttime":1401327899094,    "sourceid":"9",    "parentid":"0",    "categoryid":"",    "ownerid":"",    "metadata":"",    "comments":[        {            "cmtid":"358",            "ctime":1401327899094,            "content":"2013年8月1日18:36:29 O(∩_∩)O~",            "replyid":"0",            "user":{                "userid":"1",                "nickname":"admin",                "usericon":"",                "userurl":"",                "usermetadata":{                    "area": "北京市",                    "gender": "1",                    "kk": "",                    "level": 1                }            },            "ip":"127.0.0.1",            "useragent":"Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0",            "channeltype":"1",            "from":"",            "spcount":"",            "opcount":"",            "attachment":[                {                    "type":1,                    "desc":"",                    "url":"http://img.sohu.itc/xxxx"                }            ]        }    ]}EOT;$pingluninfo = json_decode($pingluninfo, true);if(!$pingluninfo) {    die('错误的数据格式');}echo "<pre class="prettyprint linenums">";print_r($pingluninfo);echo "

“;

输出:

Array(    [title] => 123    [url] => http://localhost/?p=9    [ttime] => 1401327899094    [sourceid] => 9    [parentid] => 0    [categoryid] =>     [ownerid] =>     [metadata] =>     [comments] => Array        (            [0] => Array                (                    [cmtid] => 358                    [ctime] => 1401327899094                    [content] => 2013年8月1日18:36:29 O(∩_∩)O~                    [replyid] => 0                    [user] => Array                        (                            [userid] => 1                            [nickname] => admin                            [usericon] =>                             [userurl] =>                             [usermetadata] => Array                                (                                    [area] => 北京市                                    [gender] => 1                                    [kk] =>                                     [level] => 1                                )                        )                    [ip] => 127.0.0.1                    [useragent] => Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0                    [channeltype] => 1                    [from] =>                     [spcount] =>                     [opcount] =>                     [attachment] => Array                        (                            [0] => Array    <div>本文来*源gaodai^.ma#com搞#代!码网</div><pre>搞gaodaima代码

( [type] => 1 [desc] => [url] => http://img.sohu.itc/xxxx ) ) ) ))

测试无误


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:怎么会有错呢,简单的json读取

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址