简体   繁体   中英

How to get each value from json method

ajax

$('#stb_no').blur(function(){
    var stb_no= $('#stb_no').val();
    $.ajax({
        url: "http://localhost/paymybill/ajax/stb_info",
        global: false,
        type: "POST",
        data: {
            'stb_no':stb_no, // you should give a key to the variable
        },
        success: function(data) {
            $('#amount').val(data);
     //     $(".email_msg").addClass("red");
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        }
    });
});

Controller code

public function stb_info(){
    $stb_no=$this->mso->alldata_stbno($this->input->post('stb_no'));
    echo json_encode($stb_no);
}

i am getting out put

[{"sxb_no":"xxxxxx","mzo_name":"xx","cto_name":"xxxxx","area":"xxxxx","name_sxb_owr":"","mobile_no":"xxxxxx","email":"xxxxx@yahoo.com","amount":"xxx"}]

i need to know how to get each values ex:- if i want get email id what i should i do in jquery please help me i new to ajax

Most browsers support JSON.parse(), which is defined in ECMA-262 and is the recommended way. Its usage is simple (I will use your example JSON):

var json = '{"area":"xxxxx",...,"email":"xxxxx@yahoo.com","amount":"xxx"}';
var obj = JSON.parse(json);

Note that obj.email can't be used, because you are parsing an array.

Edit: Checking your comments you need to know that the data parameter is the JSON object, parse it first and then you can do:

$('#amount').val(obj[0].email);

Just add to $.ajax call parameter dataType:"json" and Jquery will parse it in success parameter automatically. Then use it data[0]['email'];

For example :

$('#stb_no').blur(function(){
    var stb_no= $('#stb_no').val();
    $.ajax({
        url: "http://localhost/paymybill/ajax/stb_info",
        global: false,
        type: "POST",
        data: {
            'stb_no':stb_no, // you should give a key to the variable
        },
        success: function(data) {
            $('#email').val(data[0]['email']);

            //OR
            var obj = JSON.parse(data);
            $('#email').val(obj[0].email);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        }
    });
});

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