简体   繁体   English

解析形成良好的json字符串

[英]parsing well formed json string

I have the following well formed JSON string 我有以下格式良好的JSON字符串

{"paymentType": 1, "payer": john doe, "currency": 1, "receiptId": 4, "amount": [{"invoiceFeeId": 12080, "amountPaid": 120},{"invoiceFeeId": 12079, "amountPaid": 200},{"invoiceFeeId": 12078, "amountPaid": 500}] }

However i can't seem to parse it into a javascript object before submiting a form like this. 但是,在提交这样的表单之前,我似乎无法将其解析为javascript对象。

var paymentFormSearialized = '{"paymentType": '+$("#paymentForm #paymentType").val()+', "payer": '+$("#paymentForm #payer").val()+
                               ', "currency": '+$("#paymentForm #currency").val()+', "receiptId": '+$("#receiptId").val()+', "amount": ['+amountsasjson+'] }';

    $.post("<c:url value='/payments/create/${invoiceId}'/>", $.parseJSON(paymentFormSearialized ), function(data){
         alert(data);
    }); 

The post data must be a JSON object, out of desperation I even tried using eval(). post数据必须是一个JSON对象,出于绝望,我甚至尝试使用eval()。

Please help, maybe I am not seeing someting in my string? 请帮忙,也许我没有看到我的字符串?

That is not well formed, you have unquoted strings. 那个形式不好,你有不带引号的字符串。 Besides why do you need to make a clumsy string and parse it into an object, when you can make an object to begin with? 除此之外,为什么你需要制作一个笨拙的字符串并将其解析为一个对象,当你可以创建一个对象时?

var data = {
    paymentType: +$("#paymentForm #paymentType").val(),
    payer: $("#paymentForm #payer").val(),
    currency: $("#paymentForm #currency").val(),
    receiptId: $("#receiptId").val(),
    amount: [{"invoiceFeeId": 12080, "amountPaid": 120},{"invoiceFeeId": 12079, "amountPaid": 200},{"invoiceFeeId": 12078, "amountPaid": 500}]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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