简体   繁体   English

我如何规范化/检查它可以通过foreach循环(php)运行的json对象输出

[英]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.

相关问题 我在 PHP 中有 2 个日期,如何运行一个 foreach 循环来经历所有这些日子? - I have 2 dates in PHP, how can I run a foreach loop to go through all of those days? 如何使用 for 或 foreach 循环运行 DateTime 以循环每一天并为每个循环添加 +3 天? - How can I run a DateTime with for or foreach loop to loop through each day and add +3 days to each? 我如何在 php 中循环这个数组 - How can I foreach loop this array in php PHP 无法在 foreach 循环中将数组转换为 JSON object - PHP can't convert array to JSON object in foreach loop 我在PHP中有2次时间,如何运行foreach循环来遍历所有这些日子? (最后24小时同时循环1小时!) - I have 2 time in PHP, how can I run a foreach loop to go through all of those days? (1 Hour loop same time Last 24 Hours!) 如何使用 Vue 循环通过 object,类似于 PHP? - How can I loop through an object with Vue, similar to PHP? 如果foreach循环未输出任何值,如何运行一段代码? - How can i make a piece of code run if a foreach loop has not output any values? PHP - 当使用foreach循环遍历数组时,我怎么能判断我是否在最后一对? - PHP - when using foreach to loop through array, how can I tell if I'm on the last pair? 我如何通过php中的cURL发布JSON对象 - how can i post JSON object through cURL in php 如何使用json通过Ajax发送php对象? - How can I send a php object through ajax using json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM