javascript - 在选项中解析JSON响应值

标签 javascript jquery json ajax

我正在其中一个应用程序中处理JSON响应。我有价值,并发送到每个循环。


function ajax_to_get_products(value, row_num) {
            $.ajax({
                'url': "{% url 'get_specific_products' %}",
                'type': "GET",
                'data': {"source": value},
                'async': false,
                'success': function (data) {
                    if (data == "empty") {
                        $("#products-".concat(row_num.toString())).html("");
                        $("#products-".concat(row_num.toString())).append($('<option/>').attr("value", "None").text("-Select-").prop('selected', true).prop('disabled', true));
                    } else {
                        result = JSON.parse(data);

                        $("#products-".concat(row_num.toString())).html("");
                        $("#products-".concat(row_num.toString())).append($('<option/>').attr("value", "None").text("-Select-"));
                        $.each(result, function (i, option) {
                            var qty = option.jsonData.quantity.substring(option.items.quantity.lastIndexOf("[")+1,option.items.quantity.lastIndexOf("]"));
                            $("#products-".concat(row_num.toString())).append($('<option/>').attr({value: option.items.id, 'data-qty': qty}).text(option.items.reference_number));
                        });

                    }
                }
            });
        }


输出量

{"items": [{"description": "BLOND SIGNATURE LUMINOSITY ShampooO 250ML RETAIL", "weight": 250.0, "barcode": "366022675"]}


我需要此结果选项。

$.each(result, function (i, option) {
                            var qty = option.jsonData.quantity.substring(option.items.quantity.lastIndexOf("[")+1,option.items.quantity.lastIndexOf("]"));
                            $("#products-".concat(row_num.toString())).append($('<option/>').attr({value: option.items.id, 'data-qty': qty}).text(option.items.reference_number));
                        });

最佳答案

var jsonData = [{"item":"me","age":"30"},{"item":"you","age":"25"}];

for(var i in jsonData){
    var key = i;
    var val = jsonData[i];
    for(var j in val){
        var sub_key = j;
        var sub_val = val[j];
        console.log(sub_key);
    }
}





您可以使用以上内容。

关于javascript - 在选项中解析JSON响应值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735147/

相关文章:

java - 当我当前页面的 html 通过 Jquery 传递到托管 bean 时,primefaces remoteCommand 标记不起作用

ios - 作为 RESTful 客户端,您更喜欢什么 - RestKit 与 Resty

javascript - 此场景中 array.push 和 array.splice 之间的区别

javascript - 谷歌地图缩放功能不起作用

javascript - 幻灯片播放 2 次循环后停止?

javascript - 为什么在 HTML 5 网站上使用 CDATA?

javascript - Angular JS ng-repeat以垂直分割格式排列

php - 使用 jquery 解析 json 不起作用

javascript - 在 Three.js 中的网格上分配非插值颜色

javascript - AngularJS:在每次按键时使用动态 ng-model 模糊更新输入