php用json解析相关问题请教

jztvxx 2011-03-10 03:18:23
初次使用php做json解析。

a.php 打印出来内容如下
{"name":"bigsea","id":2}


需要用b.php解析出name 和id的值。

b.php文件如下:

$subjectjson = file_get_contents('http://192.168.0.18/a.php');

$subjectdetail = json_decode($subjectjson);
$subject = $subjectdetail -> name;
//主题信息
echo "name:".$subject -> name.'<br>';


现在的问题是b.php解析不出来name的值。请各位高人指教怎么搞的。
...全文
167 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
一起混吧 2011-03-10
  • 打赏
  • 举报
回复
给个参考:http://www.nowamagic.net/php/php_FunctionJsonEncode.php
lijpwsw 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 jztvxx 的回复:]

我要日他妈,怎么

$subjectjson = file_get_contents('http://192.168.0.18/a.php')
echo $subjectjson;

结果成了:{"name":null,"id":0}。哎
[/Quote]


说明你的a.php有问题,呵呵,慢慢调吧……
jztvxx 2011-03-10
  • 打赏
  • 举报
回复
我要日他妈,怎么

$subjectjson = file_get_contents('http://192.168.0.18/a.php')
echo $subjectjson;

结果成了:{"name":null,"id":0}。哎
lijpwsw 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 jztvxx 的回复:]

$subjectjson = '{"name":"bigsea","id":2}';
$subjectdetail = json_decode($subjectjson);

echo "name:".$subjectdetail -> name.'<br>';

直接这样可以出来。 用file_get_contents出不来。应该就是一点点问题了。
[/Quote]


那你先把file_get_contents的结果打印出来看看
jztvxx 2011-03-10
  • 打赏
  • 举报
回复
$subjectjson = '{"name":"bigsea","id":2}';
$subjectdetail = json_decode($subjectjson);

echo "name:".$subjectdetail -> name.'<br>';

直接这样可以出来。 用file_get_contents出不来。应该就是一点点问题了。
lijpwsw 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jztvxx 的回复:]

PHP code
<?php

$subjectjson = '{"name":"bigsea","id":2}';
$subjectdetail = json_decode($subjectjson);

echo "name:".$subjectdetail -> name.'<br>';



?>



可以出来, 再给力点,把$subjectjson = file_get……
[/Quote]



还出不来吗????????
jztvxx 2011-03-10
  • 打赏
  • 举报
回复
<?php

$subjectjson = '{"name":"bigsea","id":2}';
$subjectdetail = json_decode($subjectjson);

echo "name:".$subjectdetail -> name.'<br>';



?>


可以出来, 再给力点,把$subjectjson = file_get_contents('http://192.168.0.18/a.php') 就不行了。
lijpwsw 2011-03-10
  • 打赏
  • 举报
回复
注意这两句话


$subject = $subjectdetail -> name;
//主题信息
echo "name:".$subject -> name.'<br>';



lijpwsw 2011-03-10
  • 打赏
  • 举报
回复




$subjectjson = file_get_contents('http://192.168.0.18/a.php');

$subjectdetail = json_decode($subjectjson , true);

//主题信息
echo "name:".$subjectdetail -> name.'<br>';

jztvxx 2011-03-10
  • 打赏
  • 举报
回复
楼上的大哥,$subjectjson = '{"name":"bigsea","id":2}';//也是空的。

证明是后明几句写错了,哪错了,指点下。
lijpwsw 2011-03-10
  • 打赏
  • 举报
回复
先打印你接受的字符串 , 应该是字符串没有获取到
Dleno 2011-03-10
  • 打赏
  • 举报
回复

$subjectjson = '{"name":"bigsea","id":2}';//直接这样看可以不?file_get_contents这个不太稳定,很多时候取不到值

$subjectdetail = json_decode($subjectjson);
$subject = $subjectdetail -> name;
//主题信息
echo "name:".$subject -> name.'<br>';
jztvxx 2011-03-10
  • 打赏
  • 举报
回复
php版本是5.2.8

用了json_decode($subjectjson , true);
也不行,打出来还是空的。
lijpwsw 2011-03-10
  • 打赏
  • 举报
回复


$subjectjson = file_get_contents('http://192.168.0.18/a.php');

$subjectdetail = json_decode($subjectjson , true);
$subject = $subjectdetail -> name;
//主题信息
echo "name:".$subject -> name.'<br>';

Dleno 2011-03-10
  • 打赏
  • 举报
回复
$subjectdetail的值是否正确。
先看json_encode函数是否可用?(好像PHP5.2以后的才有json_encode、json_decode)
lijpwsw 2011-03-10
  • 打赏
  • 举报
回复
$subjectdetail = json_decode($subjectjson , true);

你试试

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧