[英]How can I normalize/check a json object output it can run through a foreach loop (php)
Maybe I just need to get some sleep, but I cannot figure this one out :( ... My problem is that the json output I have changes when it contains a single order vs multiple orders. 也许我只需要睡一会儿,但我无法解决这个问题:( ...我的问题是,当json输出包含一个订单或多个订单时,我的输出发生了变化。
Here is an example json output of a single order: 这是单个订单的json输出示例:
{"Ack":"Success","OrderArray":{"Order":{"OrderID":"165921181012"}},"OrdersPerPage":"10","PageNumber":"1","ReturnedOrderCountActual":"1"}
Here is an example json output of multiple orders: 这是多个订单的json输出示例:
{"Ack":"Success","OrderArray":{"Order":[{"OrderID":"165921181012","OrderStatus":"Completed"},{"OrderID":"151330738592-1109250612005","OrderStatus":"Completed"},{"OrderID":"380931137567-501668037025","OrderStatus":"Completed"}]},"OrdersPerPage":"10","PageNumber":"1","ReturnedOrderCountActual":"3"}
The difference being the [ ] 区别在于[]
Right now my code is as follows: 现在,我的代码如下:
$json_o = json_decode($json);
foreach ($json_o->OrderArray->Order as $orderkey=>$o) { echo $o->OrderID; }
My first thought was to write an if statement to handle single orders Edit**, I can detect the difference between the single orders vs multiple orders using is_array / is_object. 我的第一个想法是编写一个if语句来处理单个订单Edit **,我可以使用is_array / is_object来检测单个订单与多个订单之间的差异。 But, is there a way to add the [ ] (force a single element array) around json "OrderID" so that the foreach loop would run even if only one order exists?
但是,是否有一种方法可以在json“ OrderID”周围添加[](强制单个元素数组),以便即使只有一个订单存在,foreach循环也可以运行?
If you are also generating the JSON yourself, normalise the format at that point. 如果您还自己生成 JSON,请在那时标准化格式。
'Order'
should always be an array of orders, even if it only contains a single element. 即使
'Order'
仅包含一个元素,也应始终是一个订单数组。 That, or set some other flag which signifies whether it contains only a single order or multiple orders. 或设置其他标志来表示它仅包含一个订单还是多个订单。
If you're only consuming the JSON and have no choice: 如果您仅使用 JSON并且别无选择:
if (!isset($json_o->OrderArray[0])) {
$json_o->OrderArray = array($json_o->OrderArray);
}
foreach ($json_O->OrderArray as $order) ...
So I found another question that dealt with a similar issue. 因此,我发现了另一个类似的问题。 php-convert-xml-to-json-group-when-there-is-one-child With this you can customize the json encode and add brackets where it is needed.
php-convert-xml-to-json-group-当有一个孩子时,您可以使用它自定义json编码并在需要的地方添加括号。 The example below adds the array around the "status" element.
下面的示例在“ status”元素周围添加数组。
<?php
/**
* PHP convert XML to JSON group when there is one child
* @link https://stackoverflow.com/q/16935560/367456
*/
$bufferXml = <<<STRING
<?xml version="1.0" encoding="UTF-8"?>
<searchResult>
<status>
<userName1>johndoe</userName1>
</status>
<users>
<user>
<userName>johndoe</userName>
</user>
<user>
<userName>johndoe1</userName>
<fullName>John Doe</fullName>
</user>
<user>
<userName>johndoe2</userName>
</user>
<user>
<userName>johndoe3</userName>
<fullName>John Doe Mother</fullName>
</user>
<user>
<userName>johndoe4</userName>
</user>
</users>
</searchResult>
STRING;
class XML2JsonSearchResult extends SimpleXMLElement implements JsonSerializable
{
public function jsonSerialize()
{
$name = $this->getName();
if ($name == 'status') {
$value = (array)$this;
return [$value];
}
return $this;
}
}
$converter = new XML2JsonSearchResult($bufferXml);
echo "<pre>";
echo json_encode($converter, JSON_PRETTY_PRINT);
echo "</pre>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.