首先很抱歉提出这个非常常见的问题,但我无法解决此错误。我有一些null值的属性。该属性为json[i].outcome_status.category
,目标是计算json结构中每个类别的出现次数,而不使用类别的null属性。我试图逃避它:
if(json[i].outcome_status.category != null)
但是,我仍然收到一个错误,即该属性为null。这是我的代码:
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['table']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getData.php",
dataType:"json",
async: false
}).responseText;
var dataTable = new google.visualization.DataTable();
dataTable.addColumn('string','category');
dataTable.addColumn('string','context');
dataTable.addColumn('number','id');
dataTable.addColumn('string','location_subtype');
dataTable.addColumn('string','location_type');
dataTable.addColumn('string','month');
dataTable.addColumn('string','persistent_id');
dataTable.addColumn('string','street name');
dataTable.addColumn('string','outcome status');
json=JSON.parse(jsonData);
var counter = 0;
var countAntiSocial = 0;
var countBurglary = 0;
var countCriminal = 0;
var countDrugs = 0;
var countOtherTheft = 0;
var countPublicDisorder = 0;
var countRobbery = 0;
var countShoplifting = 0;
var countVehicleCrime = 0;
var countViolentCrime = 0;
var countOtherCrime = 0;
for (var i=0; i<json.length; i++) {
var row = [];
row.push(json[i].category);
row.push(json[i].context);
row.push(json[i].id);
row.push(json[i].location_subtype);
row.push(json[i].location_type);
row.push(json[i].month);
row.push(json[i].persistent_id);
row.push(json[i].location.street.name);
row.push(json[i].outcome_status ? json[i].outcome_status.category : "null");
dataTable.addRow(row);
//escaping the nulls
if(json[i].outcome_status.category != null) {
if(json.outcome_status.category == "anti-social-behavior"){
countAntiSocial = countAntisocial + 1;
}
if(json[i].outcome_status.category == "burglary"){
countBurglary = countBurglary + 1
}
if(json[i].outcome_status.category == "criminal-damage-arson"){
countCriminal = countCriminal + 1;
}
if(json[i].outcome_status.category == "drugs"){
countDrugs = countDrugs + 1;
}
if(json[i].outcome_status.category == "other-theft"){
countOtherTheft = countOtherTheft + 1;
}
if(json[i].outcome_status.category == "public-disorder-weapons"){
countPublicDisorder = countPublicDisorder + 1;
}
if(json[i].outcome_status.category == "robbery"){
countRobbery = countRobbery + 1;
}
if(json[i].outcome_status.category == "shoplifting"){
countShopLifting = countShopLifting + 1;
}
if(json[i].outcome_status.category == "vehicle-crime"){
countVehicleCrime = countVehicleCrime + 1;
}
if(json[i].outcome_status.category == "violent-crime"){
countViolentCrime = countViolentCrime + 1;
}
if(json[i].outcome_status.category == "other-crime"){
countOtherCrime = countOtherCrime + 1;
alert(countOtherCrime);
}
}
}
var chart = new google.visualization.Table(document.getElementById('chart_div'));
chart.draw(dataTable, {width: 1000, height: 300});
}
</script>
和我的PHP:
<?php
$json = file_get_contents('http://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2013-01');
$json = str_replace("\xe2\x80\xa8", '\\u2028', $json);
$json = str_replace("\xe2\x80\xa9", '\\u2029', $json);
echo $json;
?>
如果有人能发现什么是错误,那将是值得赞赏的。干杯
答案 0 :(得分:1)
问题是json[i].outcome_status
未定义,然后您尝试访问该属性。没有category
属性可以拥有值,因为您希望该属性存在的对象本身不存在。
你在这处理:
row.push(json[i].outcome_status ? json[i].outcome_status.category : "null");
因为该代码本质上是:如果json[i].outcome_status
未定义,则返回其类别,否则返回字符串"null"
。
但是,你不会在这里处理两行:
if(json[i].outcome_status.category != null) {
应该是:
if(json[i].outcome_status != null && json[i].outcome_status.category != null)
基本上,如果它具有结果状态并且具有属于真值的属性类别,请执行内部代码。
答案 1 :(得分:0)
尝试
if(!!json[i].outcome_status.category) {
not-not子句非常方便!
编辑:
row.push((!!json[i].outcome_status && !!json[i].outcome_status.category) ? json[i].outcome_status.category : "null");