PHP Json对象属性/值错误处理


PHP Json object property/value error handling

{
   "AFL Round 16":{
      "4166082":{
         "EventID":4166082,
         "ParentEventID":3744759,
         "MainEvent":"North Melbourne v Hawthorn",   
         "OutcomeDateTime":"2014-07-06 02:00:00",    
         "Competitors":{
            "Competitors":[
               {
                  "Team":"Hawthorn To Win 40+",
                  "Win":"3.00"
               }
            ],
            "ActiveCompetitors":1,
            "TotalCompetitors":1,
            "HasWinOdds":true
         },
         "EventStatus":"Open"
      },
      "4167064":{
         "EventID":4167064,
         "ParentEventID":3744759,
         "MainEvent":"North Melbourne v Hawthorn", 
         "OutcomeDateTime":"2014-07-06 02:00:00",  
         "Competitors":{
            "Competitors":[
               {
                  "Team":"Hawthorn (-5.5)",
                  "Win":"1.86"
               },
               {
                  "Team":"North Melbourne (+5.5)",
                  "Win":"1.86"
               }
            ],
            "ActiveCompetitors":2,
            "TotalCompetitors":2,
            "HasWinOdds":true
         },
         "EventStatus":"Open"
      }
  }
}

我正在使用PHP解析json对象,这里是我的json的一个样本。一切正常。我只是想检查对象属性/值是否存在,如果是,然后抛出错误,例如我想检查EventID, ParentEventID, OutcomeDateTime, Team(在竞争对手数组内)是有效的属性名称,它们不是null。

这是我的几行代码。

$SortedByDate = array();//Sorted By Date Array i.e key=EventID and value=OutcomeDateTime

//Accessing Root Element0
foreach ($json_a as $root_element => $childnode) {
    //Accessing child elements
    foreach( $childnode as $cKey => $subChild) {
        $OutcomeDateTime_UTC=gmdate('Y-m-d H:i:s', strtotime($subChild['OutcomeDateTime']));
        //checking ParentEventID=0 , Competitors array = 2 and OutcomeDateTime is greater than current time + 10 min
        if($subChild['ParentEventID']=='0' and is_array($subChild['Competitors']['Competitors']) and count ($subChild['Competitors']['Competitors'])  == 2 and $OutcomeDateTime_UTC>=$NewDateTime and !preg_match('/'(Live')/',$subChild['MainEvent']) ) {
            //Inserting values into array
            $SortedByDate[$cKey] = $subChild['OutcomeDateTime'];;
        }
    }
}

我累了添加if(isset($subChild['OutcomeDateTime']) || is_null($subChild['OutcomeDateTime']))检查属性名称是否为OutcomeDateTime,它不是null,并更改json属性的值(OutcomeDateTime)为null,但我得到一个错误,"Invalid argument supplied for foreach()"

在解析之前有更好的方法来检查属性/值吗?

试试这个,看看它是否符合您的意思。如果不是,我就不明白了。如果它确实解决了你的问题,我将解释为什么……

//Accessing Root Element0
foreach ($json_a as $root_element => &$childnode) {
    //Accessing child elements
    foreach( $childnode as $cKey => &$subChild) {
        $OutcomeDateTime_UTC=gmdate('Y-m-d H:i:s', strtotime($subChild['OutcomeDateTime']));
        //checking ParentEventID=0 , Competitors array = 2 and OutcomeDateTime is greater than current time + 10 min
        if($subChild['ParentEventID']=='0' && is_array($subChild['Competitors']['Competitors']) && count ($subChild['Competitors']['Competitors'])  == 2 && $OutcomeDateTime_UTC>=$NewDateTime && !preg_match('/'(Live')/',$subChild['MainEvent']) ) {
            //Inserting values into array
            $SortedByDate[$cKey] = $subChild['OutcomeDateTime'];
        }
        if(isset($subChild['OutcomeDateTime']) && !is_null($subChild['OutcomeDateTime'])) {
            $subChild['OutcomeDateTime'] = null;
        }
    }
}

我只是想检查对象属性/值是否存在如果是则抛出错误

你的措辞没有意义,有点奇怪,也许你说你想要验证每个键(如果它们存在),如果这些键的每个值都不是null

例如,我想检查EventID, ParentEventID, OutcomeDateTime, Team(在竞争对手数组内)是有效的属性名称,它们不是空的。

这里有一把小提琴。
$json_a = '{ "AFL Round 16":{ "4166082":{ "EventID":4166082, "ParentEventID":3744759, "MainEvent":"North Melbourne v Hawthorn", "OutcomeDateTime":"2014-07-06 02:00:00", "Competitors":{ "Competitors":[ { "Team":"Hawthorn To Win 40+", "Win":"3.00" } ], "ActiveCompetitors":1, "TotalCompetitors":1, "HasWinOdds":true }, "EventStatus":"Open" }, "4167064":{ "EventID":4167064, "ParentEventID":3744759, "MainEvent":"North Melbourne v Hawthorn", "OutcomeDateTime":"2014-07-06 02:00:00", "Competitors":{ "Competitors":[ { "Team":"Hawthorn (-5.5)", "Win":"1.86" }, { "Team":"North Melbourne (+5.5)", "Win":"1.86" } ], "ActiveCompetitors":2, "TotalCompetitors":2, "HasWinOdds":true }, "EventStatus":"Open" } }}';
$json_a = json_decode($json_a, true);
$json_a = reset($json_a); // ignore these parts since you already know how to get them
$errors = array();
$valid_keys = array('EventID', 'ParentEventID', 'OutcomeDateTime', 'MainEvent', 'Competitors', 'EventStatus');
foreach($json_a as $event_id => $values) {
    // check for keys
    $keys = array_keys($values);
    foreach($valid_keys as $key) {
        if(!in_array($key, $keys)) {
            // check keys, not valid if it goes here
            $errors[] = "<b>$key</b> is missing on your data <br/>";
        } else {
            // valid keys, check values
            if(empty($values[$key])) {
                // empty values
                $errors[] = "<b>$key</b> has an empty value <br/>";
            }
        }
    }
    // next checking, competitors
    foreach($values['Competitors']['Competitors'] as $competitors) {
        if(empty($competitors)) {
            // if competitors is empty
            $errors[] = "<b>Competitors</b> has an empty value <br/>";
        }
    }
}
if(!empty($errors)) {
    // not a good error triggering device, just change this to something else
    trigger_error('<pre>'.implode($errors).'</pre>', E_USER_ERROR);
}