我正在通过eBay API响应解析。我想将其返回给网站更整洁,并更易于使用JavaScript解析。我成功地通过XML进行了解析...但是现在将其转换为JSON以重新发送回客户端让我有些头疼。
注意:$resp
是eBay的回复。它们的全长XML已成功与下面的代码一起解析。
例如 ... $valueName
可能是Grade
。然后,我进入下一个foreach循环并获取该值。这些values
可能是10, 9.5, 9
etc 。
这是我的PHP代码。
$arrayName = array();
$arrayValue = array();
foreach($resp->aspectHistogramContainer->aspect as $name) {
$nameAspect = $name['name'];
//$arrayName["aspectName"] = $nameAspect;
foreach($name->valueHistogram as $value) {
$valueAspect = $value['valueName'];
//$arrayValue["aspectValue"] = $valueAspect;
}
//array_push($arrayName, $arrayValue);
}
echo json_encode($arrayName);
因此,无需我尝试创建自己的JSON,我就能获得所需的东西。我回应了结果,它与此相似...
NAME
-----值
-----值
-----值
NAME
-----值
NAME
等等等
对于JSON响应...我正在寻找类似的东西...
[
{
"name": "NAME",
"value": ["value", "value"]
}, {
"name": "name",
"value": ["value", "value"]
}
]
任何帮助和指导将不胜感激。
eBay的响应是这样的(还有更多<aspect>
和<valueHistogram>
)
<getHistogramsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<ack>Success</ack>
<version>1.13.0</version>
<timestamp>2018-11-07T15:32:20.380Z</timestamp>
<aspectHistogramContainer>
<domainDisplayName>Baseball Cards</domainDisplayName>
<aspect name="Card Manufacturer">
<valueHistogram valueName="Ace Authentic">
<count>19</count>
</valueHistogram>
<valueHistogram valueName="American Caramel">
<count>2024</count>
</valueHistogram>
<valueHistogram valueName="APBA">
<count>10554</count>
</valueHistogram>
<valueHistogram valueName="Bazooka">
<count>8826</count>
</valueHistogram>
<valueHistogram valueName="Be A Player">
<count>17</count>
</valueHistogram>
<valueHistogram valueName="Bell Brand Dodgers">
<count>334</count>
答案 0 :(得分:3)
要对其进行编码(并假设为SimpleXML),这只是构建每个内部$aspect
数据数组,然后将值添加到其中的一种情况。我使用(string)
来确保数据没有存储为SimpleXMLElement,这可能会导致副作用...
$arrayName = array();
foreach($resp->aspectHistogramContainer->aspect as $name) {
$aspect = [ "name" => (string)$name['name']];
foreach($name->valueHistogram as $value) {
$aspect["value"][] = (string)$value['valueName'];
}
$arrayName[] = $aspect;
}
echo json_encode($arrayName);
使用示例XML,这给出了...
[{"name":"Card Manufacturer","value":["Ace Authentic","American Caramel","APBA","Bazooka","Be A Player","Bell Brand Dodgers"]}]
答案 1 :(得分:2)
创建一个单个数组$resultArray
并将值存储在其中。通过以最小的更改维护当前代码结构,这是更新的代码段,
$resultArray = array();
$i = 0; // Maintain Array Index value
foreach($resp->aspectHistogramContainer->aspect as $name) {
$resultArray[$i]["aspectName"] = (string)$name['name'];;
foreach($name->valueHistogram as $value) {
$resultArray[$i]["aspectValue"][] = (string)$value['valueName'];
}
$i++; // Increment array index to store next value
}
echo json_encode($resultArray);
答案 2 :(得分:1)
$results = array();
// Parse the XML into a keyed array
foreach($resp->aspectHistogramContainer->aspect as $name) {
$nameAspect = (string) $name['name'];
$values = array();
foreach($name->valueHistogram as $value) {
$values[] = (string) $value['valueName'];
}
$results[$nameAspect] = $values;
}
// This keeps things simple - rewrite to the required JSON format
$outputForJSON = array();
foreach ($results as $name => $values) {
$outputForJSON[] = array(
"name" => $name,
"values" => $values
);
}
echo json_encode($outputForJSON);