如何遍历JSON属性不知道,如果是数组或不是? [英] how to iterate over JSON property not knowing if it's array or not?

查看:191
本文介绍了如何遍历JSON属性不知道,如果是数组或不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题,其中一个API响应我DEPARTURESEGMENT有时只包含一个对象,有时包含对象的数组。根据这种情况下它,我似乎需要在我的foreach循环不同的逻辑。

I have this problem where an API responds to me with DEPARTURESEGMENT sometimes containing only one object, and sometimes containing an array of objects. Depending on which case it is, I seem to need different logics in my foreach-loop.

响应答:

{
  "getdeparturesresult":{
     "departuresegment":[{
        "departure":{
           "location":{
              "@id":"7461018",
              "@x":"12.523958",
              "@y":"57.938402",
              "name":"Noltorps centrum"
           },
           "datetime":"2014-12-04 23:05"
        },
        "direction":"Alingsås station",
        "segmentid":{
           "mot":{
              "@displaytype":"B",
              "@type":"BLT",
              "#text":"Buss"
           },
           "carrier":{
              "name":"Västtrafik",
              "url":"http://www.vasttrafik.se/",
              "id":"279",
              "number":"1"
           }
        }
     },
     {
        "departure":{
           "location":{
              "@id":"7461018",
              "@x":"12.523958",
              "@y":"57.938402",
              "name":"Noltorps centrum"
           },
           "datetime":"2014-12-04 23:05"
        },
        "direction":"Alingsås station",
        "segmentid":{
           "mot":{
              "@displaytype":"B",
              "@type":"BLT",
              "#text":"Buss"
           },
           "carrier":{
              "name":"Västtrafik",
              "url":"http://www.vasttrafik.se/",
              "id":"279",
              "number":"1"
           }
        }
     }
     ]
  }
}

工程与这个循环:

Works with this loop:

foreach ($apiData->getdeparturesresult->departuresegment as $m) {

虽然这种反应乙:

While this response B:

{
  "getdeparturesresult":{
     "departuresegment":{
        "departure":{
           "location":{
              "@id":"7461018",
              "@x":"12.523958",
              "@y":"57.938402",
              "name":"Noltorps centrum"
           },
           "datetime":"2014-12-04 23:05"
        },
        "direction":"Alingsås station",
        "segmentid":{
           "mot":{
              "@displaytype":"B",
              "@type":"BLT",
              "#text":"Buss"
           },
           "carrier":{
              "name":"Västtrafik",
              "url":"http://www.vasttrafik.se/",
              "id":"279",
              "number":"1"
           }
        }
     }
  }
}

需要这样一个循环(否则它抛出一个错误):

needs a loop like this (otherwise it throws an error):

foreach ($apiData->getdeparturesresult as $m) {

有没有写环路故障安全的DEPARTURESEGMENT是对象数组或只是一个对象(括号[]是的JSON权结构的唯一区别?),或者我必须以某种方式试验和方式最先看到的DEPARTURESEGMENT是否根据结果是数组,并派遣到两个不同的循环?

Is there a way to write the loop failsafe for whether DEPARTURESEGMENT is an array of objects or just one object (the brackets [] is the only difference to the structure of the json right?) or do I have to somehow test and see first whether DEPARTURESEGMENT is an array or not, and dispatch to two different loops depending on the outcome?

推荐答案

您有几个方法可以帮助你:

You have a few methods that can help you:

  • is_array
  • is_object
  • instanceof // if you receive specific object
  • gettype
  • json_decode second parameter, which if is set to true, tries to decode the json as an array

在你的情况,你很可能会通过执行以下罚款:

In you situation, you would probably be fine by doing the following:

if (is_object($entry)) {
    handleObject($entry);
} elseif (is_array($entry) && count($entry)) {
    foreach ($entry as $e) {
        handleObject($e);
    }
}

这篇关于如何遍历JSON属性不知道,如果是数组或不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆