【问题标题】:Select parts of JSON and check if value is empty选择部分 JSON 并检查值是否为空
【发布时间】:2013-05-17 06:44:25
【问题描述】:

我有一个关于选择 JSON 对象的部分并检查它们是否有值的问题。
我从 PHP 中的 API 加载 JSON:

$json_url  ='http://api.url.com/api/gateway/call/1.4/getApp?appid=2631';
$ch = curl_init($json_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$str = curl_exec($ch);
curl_close($ch);
$data = json_decode($str);

数据输出为:

object(stdClass)[1]
      public 'app' => 
        array
          0 => 
            object(stdClass)[2]
              public 'id' => string '2631' (length=4)
              public 'apptypeid' => string '3' (length=1)
              public 'organizerid' => string '913' (length=3)
              public 'accountId' => string '687' (length=3)
              public 'name' => string 'Meet in Liege' (length=13)
              public 'info' => string '' (length=0)
              public 'submit_description' => string '' (length=0)
              public 'category' => string '' (length=0)
              public 'app_icon' => string 'upload/appimages/2631/icon1024.png' (length=34)
              public 'homescreen' => string '' (length=0)
              public 'token' => string 'e6615c94a8b0fed28cdbec611669b19b' (length=32)
              public 'certificate' => string 'app2631' (length=7)
              public 'subdomain' => string 'mil13' (length=5)
              public 'cname' => string '' (length=0)
              public 'advancedFilter' => string '0' (length=1)
              public 'status' => string 'inactive' (length=8)
              public 'isdeleted' => string '0' (length=1)
              public 'creation' => string '2013-03-19 14:20:44' (length=19)
              public 'defaultlanguage' => string 'fr' (length=2)
              public 'visibleintapcrowd' => string '0' (length=1)
              public 'applestoreid' => string '0' (length=1)
              public 'address' => string '' (length=0)
              public 'lat' => string '0' (length=1)
              public 'lon' => string '0' (length=1)
              public 'telephone' => string '' (length=0)
              public 'email' => string '' (length=0)
              public 'website' => string '' (length=0)
              public 'contentmodule_css_phone' => string '' (length=0)
              public 'contentmodule_css_tablet' => string '' (length=0)
              public 'searchterms' => string '' (length=0)
              public 'fbid' => string '563672167017349' (length=15)
              public 'dropdb' => string '0000-00-00 00:00:00' (length=19)
              public 'bundle' => string 'com.tapcrowd.meetinliege2631' (length=28)
              public 'blankurl' => string '' (length=0)
              public 'header_html_phones' => string '' (length=0)
              public 'header_html_tablets' => string '' (length=0)
              public 'footer_html_phones' => string '' (length=0)
              public 'footer_html_tablets' => string '' (length=0)
              public 'themeid' => string '41' (length=2)
              public 'timestamp' => string '1368548676' (length=10)
              public 'subflavorid' => string '12' (length=2)
              public 'subflavortype' => string '3' (length=1)
              public 'subflavorpaidtype' => string '9999' (length=4)
              public 'show_external_ids_in_cms' => string '0' (length=1)
              public 'launcherview' => string '' (length=0)
              public 'responsive' => string '1' (length=1)
              public 'apptype' => string 'Event Flavor' (length=12)
              public 'channel' => string '1' (length=1)
              public 'aboutbuttonkey' => string 'About TapCrowd' (length=14)
              public 'aboutbuttonurl' => string 'http://m.tap.cr/about' (length=21)
      public 'appversion' => string '' (length=0)
      public 'events' => 
        array
          0 => 
            object(stdClass)[3]
              public 'id' => string '5004' (length=4)
              .......

现在我想检查 app 数组的“info”、“app_icon”、“address”和“email”是否为空。
有谁知道我该怎么做? JSON 查询可以做到这一点吗?我想用javascript做我的操作!

【问题讨论】:

    标签: php javascript arrays json curl


    【解决方案1】:

    你就不能这样做吗:

    if(trim($data->app[0]->info)=='' || is_null($data->app[0]->info)){
      echo "info empty";
    }
    
    if(trim($data->app[0]->app_icon)=='' || is_null($data->app[0]->app_icon)){
      echo "app_icon empty";
    }
    
    if(trim($data->app[0]->address)=='' || is_null($data->app[0]->address)){
      echo "address empty";
    }
    
    if(trim($data->app[0]->email)=='' || is_null($data->app[0]->email)){
      echo "email empty";
    }
    

    【讨论】:

    • 这不起作用,因为$data 是一个对象,而您将它用作数组。
    【解决方案2】:

    你试过这个吗:

    if(empty($data->app[0]->info)){
        // info empty
    }
    if(empty($data->app[0]->app_icon)){
        // app_icon empty
    }
    if(empty($data->app[0]->address)){
        // address empty
    }
    if(empty($data->app[0]->email)){
        // email empty
    }
    

    如果您将 json 传递给 javascript,则使用(假设 json 分配给名为 data 的变量):

    if(data.app[0].info.length === 0){
        // info empty
    }
    

    然后通过将 info 替换为您要检查的名称来重复其他操作。

    【讨论】:

    • 你知道我如何在 js 中做到这一点吗?
    • 使用正确的标头等将 json 传递给 javascript,然后使用:if(data.app[0].info.length === 0){ /* empty */ }
    【解决方案3】:

    json_decode 返回一个普通的 PHP 对象,所以应该很容易。如果我没记错的话,考虑到您的 $data 对象的结构,这可以做到:

    // Retrieve the app object from $data
    $app = $data->app[0];
    
    // Check whatever you want with the object elements
    if empty($app->info) "info empty\n";
    if empty($app->app_icon) "app_icon empty\n";
    if empty($app->address) "address empty\n";
    if empty($app->email) "email empty\n";
    

    如果数组中有更多 app 对象条目,您可以考虑使用 foreach 来处理它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      相关资源
      最近更新 更多