如何将Json文件中的值分配给Javascript数组

时间:2018-09-18 17:35:15

标签: javascript jquery arrays json typeahead.js

要在我手动声明的typeahead search中填充国家/地区列表。

var data = [
    {
        "id": "Austria",
        "continent": "Europe" 
    }, 
         ...
    { 
        "id": "USA",
        "continent": "America" 
    }
];

我希望数组data从外部json文件中获取数据。假设我的countries.json文件包含此数据

[
    {
        "id": "Austria",
        "continent": "Europe"
    }, 
    {
        "id": "France",
        "continent": "Europe"
    }, 
    {
        "id": "Japan",
        "continent": "Asia"
    }, 
    {
        "id": "USA",
        "continent": "America"
    }
]

我使用getJSON将json数据加载到数组data

var data = [];
$.getJSON( "js/countries.json", function(json){
  data = json;
  console.log(data);
});

控制台向我显示data包含我需要的值,但是在我将data用作源代码的其余代码中,它不起作用。当我从Json文件加载数据时,搜索输入中没有显示任何国家。

任何建议,请问我缺少什么?谢谢。

var data = [{
  "id": "Austria",
  "continent": "Europe"
}, {
  "id": "France",
  "continent": "Europe"
}, {
  "id": "Japan",
  "continent": "Asia"
}, {
  "id": "USA",
  "continent": "America"
}];


typeof $.typeahead === 'function' && $.typeahead({
  input: ".js-typeahead-input",
  minLength: 0,
  hint: true,
  searchOnFocus: true,

  group: {
    key: "continent",
    template: function(item) {
      var continent = item.continent;
      return continent;
    }
  },
  emptyTemplate: 'no result for {{query}}',
  groupOrder: ["Europe", "Asia", "America"],
  display: ["id"],
  correlativeTemplate: true,
  dropdownFilter: [{
    key: 'continent',
    template: '<strong>{{continent}}</strong> continent',
    all: 'All Countries'
  }],
  multiselect: {
    matchOn: ["id"],
    data: function() {
      var deferred = $.Deferred();
      return deferred;
    }
  },

  template: '<span>' +
    '<span class="id">{{id}}</span>' +
    '</span>',
  source: {
    groupName: {
      data: data
    }
  },
  debug: true
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-typeahead/2.10.6/jquery.typeahead.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-typeahead/2.10.6/jquery.typeahead.js"></script>

<div class="typeahead__container">
  <div class="typeahead__field">
    <span class="typeahead__query">
                <input class="js-typeahead-input"
                       name="q"
                       type="search"
                       autofocus
                       autocomplete="on">
            </span>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

尝试

data = JSON.parse(json);

请求将获取文本,您需要将该文本解析为JavaScript对象。看看我如何将代码段中的对象转换为需要解析的字符串。试试

console.log(typeof json)

,您将看到它是一个字符串,而不是数组。

var data = JSON.parse(`[{
  "id": "Austria",
  "continent": "Europe"
}, {
  "id": "France",
  "continent": "Europe"
}, {
  "id": "Japan",
  "continent": "Asia"
}, {
  "id": "USA",
  "continent": "America"
}]`);


typeof $.typeahead === 'function' && $.typeahead({
  input: ".js-typeahead-input",
  minLength: 0,
  hint: true,
  searchOnFocus: true,

  group: {
    key: "continent",
    template: function(item) {
      var continent = item.continent;
      return continent;
    }
  },
  emptyTemplate: 'no result for {{query}}',
  groupOrder: ["Europe", "Asia", "America"],
  display: ["id"],
  correlativeTemplate: true,
  dropdownFilter: [{
    key: 'continent',
    template: '<strong>{{continent}}</strong> continent',
    all: 'All Countries'
  }],
  multiselect: {
    matchOn: ["id"],
    data: function() {
      var deferred = $.Deferred();
      return deferred;
    }
  },

  template: '<span>' +
    '<span class="id">{{id}}</span>' +
    '</span>',
  source: {
    groupName: {
      data: data
    }
  },
  debug: true
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-typeahead/2.10.6/jquery.typeahead.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-typeahead/2.10.6/jquery.typeahead.js"></script>

<div class="typeahead__container">
  <div class="typeahead__field">
    <span class="typeahead__query">
                <input class="js-typeahead-input"
                       name="q"
                       type="search"
                       autofocus
                       autocomplete="on">
            </span>
  </div>
</div>