0

我已从我的 MySQL 数据库中获取值并将其存储为 JSON 格式。现在我要做的是我需要在 JvectorMap 上显示这些值。但我做不到。

这是我的 PHP 代码:

$json_data=array(); 

foreach($result as $rec)  
{  
$json_array['Country']=$rec['user_country'];  
 $json_array['CountryCode']=$rec['country_code']; 
$json_array['persons']=$rec['usercountry']; 

array_push($json_data,$json_array);  
}  echo json_encode($json_data) ;

我收到的 JSON 是:

[
  {"Country":"Australia","CountryCode":"AU","persons":"5"}, 
  {"Country":"Spain","CountryCode":"ES","persons":"2"}, 
  {"Country":"India","CountryCode":"IN","persons":"8"}, 
  {"Country":"Mexico","CountryCode":"MX","persons":"4"},
  {"Country":"United States","CountryCode":"US","persons":"4"}
]

我正在使用的脚本是:

<script type="text/javascript">
var dataC = '<?php echo $data  ; ?>' ;
var countryData = {};
$.each(dataC, function() {
countryData[this.CountryCode] = this.persons;
});

$(function() {
 $('#world-map').vectorMap({
    map: 'world_mill_en',
    series: {
        regions: [{
            values: countryData, //load the data
            scale: ['#C8EEFF', '#0071A4'],
            normalizeFunction: 'polynomial'}]
    },
    //-----------------------------------------------
    // changed to onRegionLabelShow from onLabelShow
    //-----------------------------------------------
    onRegionLabelShow: function(e, el, code) {
        //search through dataC to find the selected country by it's code
        var country =$.grep(dataC, function(obj, index) {
            return obj.CountryCode == code;
        })[0]; //snag the first one
        //only if selected country was found in dataC
        if (country != undefined) { 
           el.html(el.html() + 
                "<br/><b>Code: </b>" +country.CountryCode + 
                "<br/><b>Name: </b>" + country.Country + 
                "<br/><b>persons: </b>" + country.persons);
        }
    }
});
});​
</script>

但我收到错误:

" jquery.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search 'length' in [{"Country":"Australia","CountryCode":"AU","persons":"5"} ,{"Country":"Spain","CountryCode":"ES","persons":"2"},{"Country":"India","CountryCode":"IN","persons":"8 "},{"Country":"Mexico","CountryCode":"MX","persons":"4"},{"Country":"United States","CountryCode":"US","persons" :"4"}]"

4

0 回答 0