简体   繁体   中英

How to get particular value from json data fetched from database?

Hello i am having issues in showing a particular value from the JSON data that i fetched from database i want to show only the product name on the input text how to do it?? i tried but it says undefied value.

Here is my JSON code :-

{"brcode":[{"id":"1","name":"Rolex Watch","description":"Rolex wrist watches","uom":"1000","brand":"1051","tax_id":"1","purchase_price":"5000","sale_price":"6000","barcode":"ROLEX5000","created_date":"2017-05-29 13:31:24","created_by":"1","updated_date":"2017-05-29 13:31:24","updated_by":"1","status":null},{"id":"3","name":"motorola X play","description":"Moto X play mobile phone","uom":"50","brand":"7845","tax_id":"1","purchase_price":"20000","sale_price":"25000","barcode":"MOTOXPLAY500","created_date":"2017-05-29 14:18:43","created_by":"0","updated_date":"2017-05-29 14:18:43","updated_by":"0","status":null},{"id":"4","name":" LG Smart LED TV","description":"Smart LED TV from LG","uom":"5","brand":"5420","tax_id":"1","purchase_price":"80000","sale_price":"100000","barcode":"LGSMART5012","created_date":"2017-05-29 15:39:35","created_by":"0","updated_date":"2017-05-29 15:39:35","updated_by":"0","status":null},{"id":"5","name":"Computer Intel","description":"Intel smart computer","uom":"1","brand":"1","tax_id":"1","purchase_price":"40000","sale_price":"50000","barcode":"INTELPC2123","created_date":"2017-05-29 17:59:31","created_by":"0","updated_date":"2017-05-29 17:59:31","updated_by":"0","status":null},{"id":"6","name":"DDR4 Ram","description":"Ram for computer","uom":"6","brand":"1","tax_id":"1","purchase_price":"4000","sale_price":"5000","barcode":"RAMDDR4","created_date":"2017-05-29 18:15:17","created_by":"0","updated_date":"2017-05-29 18:15:17","updated_by":"0","status":null}]}

Here is my function :-

function get_barcodes() {


     $.ajax({
        url: 'http://localhost/retail/main/ajax_barcodes/',
        type: 'POST',
        datatype: 'json',
        data: { 'barcode': $('#brcode option:selected').val() },
        success: function (data) {
           // var brcodes = JSON.parse(data);
            //console.log(brcodes);          
            //var brCodes = JSON.parse(data);
           //var json = JSON.parse(data);
            //console.log(brCodes.brcode);
            //$("#product_items_opts").val(barcodes.brcode.name);
           // console.log(json);

           console.log(data);

        }
    });

    // $('#prdct_barcode[' + product_barcode_counter + ']').each(function(){
    //      $('#prdct_barcode[' + product_barcode_counter + ']').change(function(){



    //      });
    // });


}

please ignore the commented part that i wrote just to check the values. Please tell me how can i show the product name from JSON data?

Try this

var jsonData = JSON.parse(data);
for(var i=0; i<jsonData.brcode.length; i++) {
   var product = jsonData.brcode[i];
   console.log(product.name);
}

Here is a basic for loop to loop through the JSON to get the names and barcodes of the products. This should help you to achieve what you want to do. Put this code in your success function where you console log the data.

var jsonData = JSON.parse(data);
for(var i = 0; i < jsonData.brcode.length; i++){
     console.log(jsonData.brcode[i].name);
     console.log(jsonData.brcode[i].barcode);
}

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