我试图通过解析xml文件在php中使用字符串手动创建json。我创造了整个json,但是当我验证它时;我可以看到之间不必要的换行符导致验证失败。 这是我的json的一部分。
"basicInfo": {
"title": "Mini Figures Heroes Assemble Building Toys Taskmaster Martain Storm Thor #v6lo01",
"viewItemURL": "http
://www.ebay.com/itm/Mini-Figures-Heroes-Assemble-Building-Toys-Taskmaster-Martain-Storm-Thor-v6lo01-
/351370851384?pt=LH_DefaultDomain_0",
"galleryURL": "http://thumbs1.ebaystatic.com/m/myYu_MI3-mPtA6s3bJboaVA
/140.jpg",
"pictureURLSuperSize": "http://i.ebayimg.com/00/s/OTAwWDEzNTI=/z/lQEAAOSwqu9VJ-Fy/$_3.JPG",
"convertedCurrentPrice": "7.76",
"shippingServiceCost": "0.0",
"conditionDisplayName": "New",
"listingType": "Auction",
"location": "China",
"categoryName": "Comic Book Heroes",
"topRatedLising": "false"
},
正如您所看到的那条线正在破坏网址。
在php代码中,我只是直接连接。
$resultxml.='"viewItemURL":"'.$grandchild->viewItemURL;
$resultxml.='",';
$resultxml.='"galleryURL":"'.$grandchild->galleryURL.'",';
我不知道我做错了什么。
答案 0 :(得分:0)
使用json_encode。一个例子:
<?php
/*
If $grandchild->viewItemURL = "http
://www.ebay.com/itm/Mini-Figures-Heroes-Assemble-Building-Toys-Taskmaster-Martain-Storm-Thor-v6lo01-/351370851384?pt=LH_DefaultDomain_0"
Value whith carriage return
*/
$data = array(
"basicInfo" => array(
"title" => $grandchild->title,
"viewItemURL" => str_replace( "\n", '', $grandchild->viewItemURL ),
"galleryURL" => str_replace( "\n", '', $grandchild->galleryURL ),
"pictureURLSuperSize" => str_replace( "\n", '', $grandchild->pictureURLSuperSize ),
"convertedCurrentPrice" => $grandchild->convertedCurrentPrice,
// .
// . and more items
// .
)
);
$dataJson = json_encode( $data );
?>
答案 1 :(得分:0)
我同意这些意见,你不应该手动创建它。
然而,要回答你的问题......
我复制了你的JSON并查看了它,所有的URL在http://之间都有一个空格。
&#34; viewItemURL&#34;:&#34; http://www.ebay.com/itm/Mini [...]&#34;,
&#34; galleryURL&#34;:&#34; http://thumbs1.ebaystatic.com [...]&#34;,
正如您所看到的,每个冒号后都有一个空格&#34;:&#34;,即冒号和JSON值之间。 我猜你有另一个功能(这里没有发布),它在每个冒号后添加了空格,它也为URL做了这个。