【问题标题】:How to get particular value from json data fetched from database?如何从从数据库中获取的 json 数据中获取特定值?
【发布时间】:2017-05-31 18:39:01
【问题描述】:

您好,我在显示从数据库中获取的 JSON 数据中的特定值时遇到问题,我只想在输入文本上显示产品名称怎么办?我试过了,但它说价值不菲。

这是我的 JSON 代码:-

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

这是我的功能:-

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(){



    //      });
    // });


}

请忽略我为检查值而编写的注释部分。请告诉我如何从 JSON 数据中显示产品名称?

【问题讨论】:

  • 您想获取所有产品名称并在某处显示它们吗?
  • 是的,我也想在选择菜单的选项中显示所有条形码,我想附加所有条形码选项。

标签: jquery json ajax codeigniter


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    这是一个基本的 for 循环,用于遍历 JSON 以获取产品的名称和条形码。这应该可以帮助您实现您想要做的事情。将此代码放入您的成功函数中,您可以在其中控制台记录数据。

    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);
    }
    

    【讨论】:

    • 先尝试用JSON.parse解析数据。
    猜你喜欢
    • 1970-01-01
    • 2019-03-30
    • 2023-03-04
    • 2023-03-22
    • 2019-06-16
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多