1

I am using the following code

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

I want to assign the each responce value in each variable Ex:clientName="dxx";cleitPhone="eerere"

I have the following console response:

{
    "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
}

How can I parse the above data in jQuery? Thanks.

3
  • 1
    Unclear. Please edit to add description, example and other details. Commented Sep 7, 2016 at 9:07
  • It appears as though it already is parsed...? Assuming what you've posted is actually a string, send it as a parameter to JSON.parse(). Note that if you retrieve this JSON through an AJAX request then it will be automatically parsed for you by jQuery - assuming you've set the correct MIME type in the response Commented Sep 7, 2016 at 9:08
  • 1
    Possible duplicate of Safely turning a JSON string into an object Commented Sep 7, 2016 at 9:11

2 Answers 2

1

You just need to change your success response to

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sir,its working.but how get second array value i mean taxname,taxamount in myarray
Edited my answer, please check and let me know if this is working or not.
0

Set response to variable and use the variable in your jQuery method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.