您好我正在尝试在json中生成转换整数值的代码并且没有得到任何解决方案请帮助我并解决我可能在某处错误的地方我尝试使用parseInt但是没有得到解决方案
<script>
var userId=1;
var status=2;
var siteName='qualispace.com';
var hostName='vertoz.com';
var iabCategoreisId=1;
var domainBlockId=1;
var allowPubRedirection=1;
var hostNameValidation=1;
var pricingTypeId=1;
var pricingTypeValue=2;
var frequencyCap=2;
createdDate=parseInt(123456789);
var updatedDate=123456789;
var createdId=1;
var updatedId=1;
var mobileFooterAds=1;
var mobilePageAds=1;
var mobileApp=1;
var pagead=1;
var footerad=1;
var marginad=1;
var entry_id=1;
var sizeMaster=1;
var text = '{"website":'+
'{"userId":"'+userId+'","status":"' +
status + '" ,' +
'"siteName":"'+siteName+
'","hostName":"'+hostName+'",' +
'"iabCategoreisId":"'+iabCategoreisId+'",,' +
'"domainBlockId":"'+domainBlockId+'",' +
'"allowPubRedirection":"'+allowPubRedirection+'",' +
'"hostNameValidation":"'+hostNameValidation+'",' +
'"pricingTypeId":"'+pricingTypeId+'",' +
'"pricingTypeValue":"'+pricingTypeValue+'",' +
'"frequencyCap":"'+frequencyCap+'",' +
'"createdDate":"'+createdDate+'",' +
'"updatedDate":"'+updatedDate+'",' +
'"createdId":"'+createdId+'",' +
'"updatedId":"'+updatedId+'",' +
'"mobileFooterAds":"'+mobileFooterAds+'",' +
'"mobilePageAds":"'+mobilePageAds+'",' +
'"mobileApp":"'+mobileApp+'",' +
'"pagead":"'+pagead+'",' +
'"footerad":"'+footerad+'",' +
'"marginad":"'+marginad+'"},' +
'"publications":{"entry_id":"' + entry_id + '",' +
'"sizeMaster":[{"entry_id":"' + entry_id
+ '","adType":{"entry_id":"'+entry_id+'"}}, {"entry_id":"'
+ entry_id + '","adType":{"entry_id":"' + entry_id + '"}}]}}';
alert(text);
</script>
答案 0 :(得分:2)
您应该使用JSON.stringify()
JSON.stringify()方法将值转换为JSON,如果指定了replacer函数,则可以选择替换值,或者如果指定了replacer数组,则可选地仅包括指定的属性。
实施例
var newObjcet = {
mobilePageAds: 1
};
alert(JSON.stringify(newObjcet));
&#13;
您可以创建像
这样的对象 var text = {
website: {
userId : userId,
status : status
siteName: siteName,
sizeMaster : [
{
entry_id: entry_id,
adType : {
entry_id: entry_id,
}
},
{
entry_id: entry_id,
adType : {
entry_id: entry_id,
}
}
]
}
}
答案 1 :(得分:0)
这里有重复的逗号:
'"iabCategoreisId":"'+iabCategoreisId+'",,' +
^
如果您只是修复了这个问题,那么您拥有的代码可以创建JSON,结果是一个有效的JSON字符串。
当您将字符串连接到字符串时,您不必对整数值执行任何操作,它将自动转换为字符串。
如果你想要一些整数值作为JSON中的整数而不是数字的字符串表示,你应该删除值周围的引号。
请注意,某些浏览器(例如Firefox)在警报中不会显示正确的字符串。您可以使用console.log
在日志窗口中显示字符串的内容。