简体   繁体   中英

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

 <?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

But of course building JSON yourself is not necessary. Use JSON.stringify for that.

Before you append ] to datajson , substring that value to remove last , . 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. not json object.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM