[英]how can remove last comma from json using javascript forloop
$('#save').click(function() {
var loopvalue = "<?php echo $count; ?>"
var datajson = '[';
//alert(loopvalue + "length of array")
for (i = 1; i < loopvalue; i++) {
var fetchid = '.A' + i;
var fetchid_code = '.C' + i;
datajson = datajson + "{" + "maincode :" + $('#company').val() + ",acode : " + $(fetchid_code).text() +
" , Amount :" + $(fetchid).val() + ", periodfrom :" + $('#dFrom').val() +
", periodto : " + $('#dTo').val() + ", danounc : " + $('#dano').val() +
", period : " + $('#period').val() + ", fyear : " + $('#fyear').val() +
", frequency : " + $('#freq').val() + ", stype : " + $('#stype').val() +
", sseq : " + $('#sseq').val() + " }, "
}
datajson = datajson + ']'
//console.log(datajson);
$.ajax({
type: 'POST',
url: 'jsondecode.php',
data: {
datajson: JSON.stringify(datajson)
},
//data:postArray,
dataType: "json",
success: function(data) {
console.log("success:", data);
},
failure: function(errMsg) {
console.error("error:", errMsg);
}
});
});
first i remove last comma in json
object and second when i am calling ajax
page but it is displaying NULL value in jsonencode.php
page how can I solve this problem any can suggest me this is my server site script now 首先我删除
json
对象中的最后一个逗号,然后我调用ajax
页面,但是它在jsonencode.php
页面显示NULL值如何解决这个问题任何可以建议我这是我的服务器站点脚本现在
<?php
header('Content-Type: application/json');
$data = json_decode($_POST["datajson"]);
// will echo the JSON.stringified - string:
echo $_POST["datajson"];
// will echo the json_decode'd object
var_dump($data);
//traversing the whole object and accessing properties:
foreach ($data as $Object) {
echo "maincode: " . $Object->maincode . ", Acode: " . $Object->acode . "<br/>";
}
?>
Or maybe you should use actual object instead of string-concatenation 或者您可能应该使用实际对象而不是字符串连接
Try this 试试这个
var datajson = [];
for (i = 1; i < loopvalue ; i++)
{
var fetchid = '.A' + i;
var fetchid_code = '.C' + i;
var obj = {
maincode : $('#company').val(),
acode : $(fetchid_code).text(),
Amount : $(fetchid).val(),
periodfrom : $('#dFrom').val(),
periodto : $('#dTo').val(),
danounc : $('#dano').val(),
period : $('#period').val(),
fyear : $('#fyear').val(),
frequency : $('#freq').val(),
stype : $('#stype').val(),
sseq : $('#sseq').val()
}
datajson.push( obj );
}
datajson = JSON.stringify( datajson ); //converting to string here
Write the single elements of your look into an array and then join the elements. 将外观的单个元素写入数组,然后连接元素。
See here: javascript join 看到这里: javascript加入
But of course building JSON yourself is not necessary. 但是,当然自己构建JSON是没有必要的。 Use JSON.stringify for that.
使用JSON.stringify 。
Before you append ]
to datajson
, substring
that value to remove last ,
. 您将追加前
]
到datajson
, substring
该值最后删除,
。 That is. 那是。
datajson = datajson.toString().subString(0, datajson.toString().length - 1);
Then Append ]
然后追加
]
It is not needed to use JSON.stringify(datajson)
in data
attribute of $.ajax
because you are sending the json string. 因为您要发送json字符串,所以
JSON.stringify(datajson)
在$.ajax
data
属性中使用JSON.stringify(datajson)
。 not json object. 不是json对象。
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.