如何使用PHP代码将Web图像URL编码为JSON
问题描述:
我正在编写一个应用程序,我允许用户将他们的产品详细信息输入框架[我在PHP上编写],然后通过使用该框架编码数据到JSON和我使用Web服务在Android应用程序中检索的编码数据。如何使用PHP代码将Web图像URL编码为JSON
但现在的问题是我不能够编码Web图像的URL,因此我没有在JSON得到正确的图像路径: -
像:
我给此链接编码成JSON:
"imageurl" => "http://www.libpng.org/pub/png/img_png/pnglogo-blk-sml1.png"
但每当我看到编码形式的图片网址JSON得到的东西是这样的:
"imagepath":"http:\/\/www.libpng.org\/pub\/png\/img_png\/pnglogo-blk-sml1.png"
为什么我收到错误的图像URL ....
我不知道我怎样才能解决这个issue..please有人帮助我....
albums.php:
<?php
/*
*Simple JSON generation from static array
*Author: JSR
*/
include_once './data.php';
$albums = array();
// looping through each album
foreach ($album_tracks as $album) {
$tmp = array();
$tmp["id"] = $album["id"];
$tmp["name"] = $album["album"];
$tmp["description"] = $album["detail"];
$tmp["imagepath"] = $album["imageurl"];
$tmp["songs_count"] = count($album["songs"]);
// push album
array_push($albums, $tmp);
}
//printing json
echo json_encode($albums);
?>
data.php:
<?php
/*
*Simple JSON generation from static array
*Author: JSR
*/
$album_tracks = array(
1 => array(
"id" => 1,
"album" => "Appetizers",
"detail" => "All appetizers served with mint and tamarind chutneys.",
"imageurl" => "http://www.libpng.org/pub/png/img_png/pnglogo-blk-sml1.png"
)
));
?>
答
对我来说,它的工作原理
<script>
var str="http:\/\/www.libpng.org\/pub\/png\/img_png\/pnglogo-blk-sml1.png";
document.write("<img src='" + str + "'/>");
</script>
无论何时您在json中发送图像路径或url,它都会转义所有的斜杠,引号等等。所以,您可以使用一些正则表达式像s/\ //// gis来摆脱这些转义字符并获得原始网址或位置。
这是[提供的输入完全有效的JSON结果](http://jsfiddle.net/dgnBL/2/);你想解决什么问题? – TML 2013-02-12 05:05:44
@TML我编码的json网址:http://erachnida.net/android/albums.php在我的android应用程序中,我没有在listview中获取图像...所以我想我在编码web图像网址时犯了错误..请让我知道,我在我的编码JSON获得正确的路径...? – ASMUIRTI 2013-02-12 05:12:20
JSON中的路径绝对没有错。请注意从[json.org]进行字符串解析的[railroad diagram](http://json.org/string.gif) - 您可以使用或不使用前面的反斜杠来进行solidus('/')。没有任何符合JSON的实现应该以任何方式窒息。 – TML 2013-02-12 05:13:57