[英]how to check whether NAN is present in json string or not
suppose i have below function, which is getting jsonData in the form of jason, i validate the var jsonData to check for NaN ? 假设我有以下功能,以jason的形式获取jsonData,我验证了var jsonData来检查NaN吗?
function save() {
var jsonData = getEnteredValue();
$.ajax({
type : 'POST',
url : 'saveSalesForecast.json',
data : 'jsonPostData=' + jsonData,
success : function() { //alert("success");
}
});
}
i only know how to replace NAN but don know how to check for NAN! 我只知道如何更换NAN,但不知道如何检查NAN!
jsonData = JSON.parse(jsonData.replace(/\bNaN\b/g, "null"));
here is remaining function:(any field values can be string,numbers but it should not be NAN 这是剩余的函数:(任何字段值都可以是字符串,数字,但不能为NAN
function getEnteredValue() {
var rowIds = $("#salesForecastGrid").jqGrid('getDataIDs');
var ids=[];
var jsonPostData = "[";
for ( var i = 0; i <= rowIds.length-1; i++) {
$("#salesForecastGrid").jqGrid('editCell', i, 2, false);
var forecastedSales = parseFloat($("#salesForecastGrid")
.jqGrid('getCell', rowIds[i], 'forecastedSales'));
if (!((forecastedSales == "") || isNaN(forecastedSales) || (forecastedSales ==0))) {
if (ids.indexOf(rowIds[i])==-1){
ids.push(rowIds[i]);
}
}
}
for ( var i = 0; i <= ids.length-1; i++) {
var forecastedSales = parseFloat($("#salesForecastGrid")
.jqGrid('getCell', ids[i], 'forecastedSales'));
var id = $("#salesForecastGrid").jqGrid('getCell', ids[i],
'id');
var date = $("#salesForecastGrid").jqGrid('getCell',
ids[i], 'day');
if (id < 0) {
id = 0;
}
var record = "{" + "id:" + id + "," + "date:" + date + ","
+ "forecastedSales:" + forecastedSales + "}";
jsonPostData = jsonPostData + record;
if (i != ids.length) {
jsonPostData = jsonPostData + ",";
}
}
jsonPostData += "]";
return jsonPostData;
}
Json Data like: Json数据如下:
"[{id:68447,date:04-17-2014,forecastedSales:8420.42},{id:68448,date:04-18-2014,forecastedSales:9912.68},]"
Your problem is that you are creating the JSON manually, and thus end up with invalid JSON. 您的问题是您正在手动创建JSON,因此最终以无效的JSON结束。 Do yourself a favor and use
JSON.stringify
: 帮个忙,使用
JSON.stringify
:
function getEnteredValue() {
var rowIds = $("#salesForecastGrid").jqGrid('getDataIDs');
var ids=[];
var data = [];
// ...
for ( var i = 0; i < ids.length; i++) {
// ...
data.push(
{id: id, date: date, forecastedSales: forecastedSales}
);
}
return JSON.stringify(data);
}
Since NaN
is not a valid value in JSON, it will automatically be converted to null
. 由于
NaN
在JSON中不是有效值,因此它将自动转换为null
。 Example: 例:
> JSON.stringify({a: NaN});
"{"a":null}"
For more info see Using native JSON . 有关更多信息,请参阅使用本机JSON 。
NaN
is not acceptable in JSON. NaN
在JSON中不可接受。 JSON specification does not support NaN
as a value. JSON规范不支持
NaN
作为值。 Even when javascript object has NaN
value, it will be converted to null
when you serialise to JSON format. 即使javascript对象具有
NaN
值,当您序列化为JSON格式时,它也将转换为null
。
First of all, JSON is not an Javascript Object. 首先,JSON不是Javascript对象。 JSON is general format which can be understand by all languages.
JSON是通用格式,所有语言都可以理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.