从JSON API调用获取业务类型列表.并将它们包括在选择菜单中


fetch a list of business types from a JSON API call. and include them in a select menu

对此的修订:

<?php
$url = 'http://yoco-core-staging.herokuapp.com/api/common/v1/properties/businessCategories';
$content = file_get_contents($url);
$json = json_decode($content, true);
?>
<select>
<?php 
foreach($json['data'] as $item) {
    print '<option>'.$item[2].'</option>';
}
?>
</select>

这是我输入的新代码,用于获取显示这些选项的select语句。我现在所要做的就是创建一个函数来消除重复的条目,然后bam完成!

感谢您的所有帮助和评论

你应该试试这个。这是打印所有返回JSON的数据。

<?php
$url = 'http://yoco-core-staging.herokuapp.com/api/common/v1/properties/businessCategories';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['data'] as $item) {
    print $item[0];
    print $item[1];
    print $item[2];
    print '<br>';
}

您在代码中使用的组引用没有被标识为JSON对象中的字段名,因此您会得到未定义的错误。

JSON对象返回如下:

    array(2) {
  ["status"]=>
  int(200)
  ["data"]=>
  array(122) {
    [0]=>
    array(3) {
      [0]=>
      string(4) "5462"
      [1]=>
      string(6) "Bakery"
      [2]=>
      string(27) "Food, drink and hospitality"
    }
    [1]=>
    array(3) {
      [0]=>
      string(4) "5813"
      [1]=>
      string(3) "Bar"
      [2]=>
      string(27) "Food, drink and hospitality"
    }
    [2]=>

您可以使用对象返回的数字引用来循环遍历对象。对于您的案例,您只需回显$json[data][1]&json[data][2]foreach对象。

[请在投票前添加注释]

你可以这样做:

$.ajax({        
       url: 'http://yoco-core-staging.herokuapp.com/api/common/v1/properties/businessCategories',
       dataType: 'JSON',
       success: function(data, status) {
        //data = JSON.parse(data); this is optional depending on the type you are getting from the server
        var len = data.length;
        var html = '';
        for(var i = 0; i < len; i++){
            var id = data[i].Id;
            var title = data[i].title;
            var group = data[i].group;
            html += '<li>'+id+' - '+title+' - '+group+'</li>';
        }
        $('#somecontainer').html('<ul>'+html+'</ul>');
       },
       error: function(e) {
           console.log(e);             
       }
   });

我不知道字段的名称,但这基本上是