如何在jQuery中解析

如何在jQuery中解析

我正在使用以下代码

<script>
$(document).ready(function(){
    $('.v_modalLink').click(function(){
        var journalId=$(this).attr('data-id');
        $.ajax({
            type:"POST",
            url:"<?php echo base_url();?>index.php/pranasInventory/view_invoice/1",
            data:{journalId:journalId},
            success:function(data)
            {
                var json_obj=$.parseJSON(data);
                for(var i in json_obj)
                {
                    voucherId=json_obj[i].invoice_id;

                }

            }
        });
    })
});
</script>


我想在每个变量中分配每个响应值Ex:clientName =“ dxx”; cleitPhone =“ eerere”

我有以下控制台响应:

{
    "invoice": [{
        "invoice_id": "1",
        "client_id": "1",
        "client_name": "makemytrip",
        "client_phone": "1234567891",
        "client_email": "[email protected]",
        "client_address": "Banglore.",
        "service_name": "Mobile   Apps",
        "service_description": "asdasd asasd  asdads asdasdads.",
        "created_date": "2016-08-10",
        "gross_amount": "1000000",
        "tax_amount": "145000",
        "net_amount": "1145000",
        "tax_identifier_name": "Service Tax 14.5%"
    }],
    "myarray": [{
        "taxname": "Service tax @ 14%",
        "taxamount": 140000
    }, {
        "taxname": "swachh bharat cess @  0.5%",
        "taxamount": 5000
    }],
    "rowcount": 2
}


如何在jQuery中解析以上数据?谢谢。

最佳答案

您只需要将成功响应更改为

success:function(data)
{
    var json_obj = JSON.parse(data);

    var voucherId = json_obj['invoice'][0].invoice_id;
    var clientName = json_obj['invoice'][0].client_name;
    var clientPhone = json_obj['invoice'][0].client_phone;

    //myarray

    var taxName1 = json_obj['myarray'][0].taxname;
    var taxAmount1 = json_obj['myarray'][0].taxamount;
    var taxName2 = json_obj['myarray'][1].taxname;
    var taxAmount2 = json_obj['myarray'][1].taxamount;

    //rowcount

    var rowCount = json_obj.rowcount;
}

关于javascript - 如何在jQuery中解析JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39365679/

10-13 00:41