0

I am getting json response from ajax like this

  echo  json_encode($data);

Ajax code:

  $.ajax({
        url:"PaymentSlip/check", 
        data:{val:val},
        type: 'POST',
        success:function(ajaxresult)
        {
            $("#jgoli").html(ajaxresult);
        }
    });

Result I am getting is:

     [{"paymentId":"2","paymentLabNo":"MR-622-040618",paymentTestId":"1"}]

Now I want to access my json array in javascript by index like

      ajaxresult[0] = 2; i.e paymentId=2
      ajaxresult[1] = 2; i.e paymentLabNo=MR-622-040618

How would I achieve that?

Note: I have tried many examples on stackoverflow, I know this question must have answered earlier. But I am still stuck. Any help would be appreciated.

2
  • What are you actually trying to achieve - you want to print, on to the web page, the JSON data but in the format you used as an example? Or are you just trying to parse the data and don't understand how to access the data at all? Commented Apr 6, 2017 at 13:00
  • Yes, I want to print the data on the web page by index i.e. ajax result['paymentId'] which in my question equals to '2'. Commented Apr 6, 2017 at 13:12

4 Answers 4

1

$(document).ready(function(){
    var data =  [{"paymentId":"2","paymentLabNo":"MR-622-040618","paymentTestId":"1"}];
    //Loop on the object as key=>value
    $.each(data, function(key, value){
    //And diplay it in the result div
        $('#result').append('<p>'+data[key]['paymentId']+'</p>');
        $('#result').append('<p>'+data[key]['paymentLabNo']+'</p>');
         $('#result').append('<p>'+data[key]['paymentTestId']+'</p>');
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="result"></div>

Sign up to request clarification or add additional context in comments.

Comments

1

What you are getting is a string of encoded JSON, to use it as an object you must parse it.

Check this answer

5 Comments

ok, I parsed the json response. Now how can I access the parsed data by index?
eval() should be avoided at all costs. There are other safer methods for parsing a string to a JSON object.
You obtain an object using the mthod from above. You can iterate between the values using a code like this for( key in obj) { if(obj.hasOwnProperty(key) ) console.log(key + " is " + obj[key]) }
Yeah you should use JSON.parse as the answer states. You can use something like var obj = JSON.parse(ajaxresult) and then access it like obj[0]
solved the problem by above answer. Thanks for your time.
1
$(document).ready(function(){

    $.get("ajax_call.php", function(data){
        //console.log(data);
        var result = JSON.parse(data);
        $.each(result, function(key, value){

            $('#data').append('<tr><td>'+result[key]['username']+'</td><td>'+result[key]['email']+'</td><td>'+result[key]['mobile']+'</td></tr>');
            console.log(result[key])

        });

    });

});    

Comments

0
  $.ajax({
        url:"PaymentSlip/check", 
        data:{val:val},
        type: 'POST',
        datatype: "json", // add this
        success:function(ajaxresult)
        {
            // access return results as
            //ajaxresult[0].paymentId;
            //ajaxresult[0].paymentLabNo;
            //ajaxresult[0].paymentTestId;
            //$("#jgoli").html(ajaxresult);
        }
    });

1 Comment

solved the problem by above answer. Thanks for your time.

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.