我正在其中一个应用程序中处理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/