开发者

issue when outputting an http:// address in a sub-array with json and php

Im trying to achieve an output like this

{"status":"ok","0":{"id":"11","title":"digg","url":"http://www.digg.com"}}

but instead i am getting this

{"status":"ok","0":{"id":"11","title":"digg","url":"http:\/\/www.digg.com"}}

this is the php code im using to generate the json

$links = array('id'=>开发者_开发知识库;'11','title'=>'digg','url'=>"http://www.digg.com");
$msg = array('status'=>'ok',$links);
echo json_encode($msg);

any idea what is causing this?

UPDATE i should have been more clear if you notice the actual url, its inserting "\" before the "/" in the output. Is this supposed to happen, or is there a way to stop this?


They're both equivalent valid JSON, so it shouldn't matter. The JSON strings:

"http://www.digg.com"

and

"http:\/\/www.digg.com"

both decode to:

"http://www.digg.com"

This is a separate issue, but I would prefer:

$links = array(array('id'=>'11','title'=>'digg','url'=>"http://www.digg.com"));
$msg = array('status'=>'ok', 'links'=>$links);
echo json_encode($msg);

{"status":"ok","links":[{"id":"11","title":"digg","url":"http:\/\/www.digg.com"}]}

This makes more sense to me than having a "0" key, and it extend well if you add more sites:

$links = array(array('id'=>'11','title'=>'digg','url'=>"http://www.digg.com"),
               array('id'=>'12','title'=>'reddit','url'=>"http://www.reddit.com"));
$msg = array('status'=>'ok', 'links'=>$links);
echo json_encode($msg);

{"status":"ok","links":[{"id":"11","title":"digg","url":"http:\/\/www.digg.com"},
{"id":"12","title":"reddit","url":"http:\/\/www.reddit.com"}]}


Yes. The JSON specs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜