将 Json 编码数据转换为 javascript 数组并按索引访问值
Convert Json encoded data to javascript array and access value by index
我收到 ajax 的 json 回复,就像这样
echo json_encode($data);
Ajax代码:
$.ajax({
url:"PaymentSlip/check",
data:{val:val},
type: 'POST',
success:function(ajaxresult)
{
$("#jgoli").html(ajaxresult);
}
});
我得到的结果是:
[{"paymentId":"2","paymentLabNo":"MR-622-040618",paymentTestId":"1"}]
现在我想通过
之类的索引访问 javascript 中的 json 数组
ajaxresult[0] = 2; i.e paymentId=2
ajaxresult[1] = 2; i.e paymentLabNo=MR-622-040618
我将如何实现?
注意:我在Whosebug上试过很多例子,我知道这个问题一定早有回答。但我仍然卡住了。任何帮助将不胜感激。
您得到的是一串编码的 JSON,要将其用作对象,您必须解析它。
Check this answer
$.ajax({
url:"PaymentSlip/check",
data:{val:val},
type: 'POST',
datatype: "json", // add this
success:function(ajaxresult)
{
// access return results as
//ajaxresult[0].paymentId;
//ajaxresult[0].paymentLabNo;
//ajaxresult[0].paymentTestId;
//$("#jgoli").html(ajaxresult);
}
});
$(document).ready(function(){
var data = [{"paymentId":"2","paymentLabNo":"MR-622-040618","paymentTestId":"1"}];
//Loop on the object as key=>value
$.each(data, function(key, value){
//And diplay it in the result div
$('#result').append('<p>'+data[key]['paymentId']+'</p>');
$('#result').append('<p>'+data[key]['paymentLabNo']+'</p>');
$('#result').append('<p>'+data[key]['paymentTestId']+'</p>');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="result"></div>
$(document).ready(function(){
$.get("ajax_call.php", function(data){
//console.log(data);
var result = JSON.parse(data);
$.each(result, function(key, value){
$('#data').append('<tr><td>'+result[key]['username']+'</td><td>'+result[key]['email']+'</td><td>'+result[key]['mobile']+'</td></tr>');
console.log(result[key])
});
});
});
我收到 ajax 的 json 回复,就像这样
echo json_encode($data);
Ajax代码:
$.ajax({
url:"PaymentSlip/check",
data:{val:val},
type: 'POST',
success:function(ajaxresult)
{
$("#jgoli").html(ajaxresult);
}
});
我得到的结果是:
[{"paymentId":"2","paymentLabNo":"MR-622-040618",paymentTestId":"1"}]
现在我想通过
之类的索引访问 javascript 中的 json 数组 ajaxresult[0] = 2; i.e paymentId=2
ajaxresult[1] = 2; i.e paymentLabNo=MR-622-040618
我将如何实现?
注意:我在Whosebug上试过很多例子,我知道这个问题一定早有回答。但我仍然卡住了。任何帮助将不胜感激。
您得到的是一串编码的 JSON,要将其用作对象,您必须解析它。
Check this answer
$.ajax({
url:"PaymentSlip/check",
data:{val:val},
type: 'POST',
datatype: "json", // add this
success:function(ajaxresult)
{
// access return results as
//ajaxresult[0].paymentId;
//ajaxresult[0].paymentLabNo;
//ajaxresult[0].paymentTestId;
//$("#jgoli").html(ajaxresult);
}
});
$(document).ready(function(){
var data = [{"paymentId":"2","paymentLabNo":"MR-622-040618","paymentTestId":"1"}];
//Loop on the object as key=>value
$.each(data, function(key, value){
//And diplay it in the result div
$('#result').append('<p>'+data[key]['paymentId']+'</p>');
$('#result').append('<p>'+data[key]['paymentLabNo']+'</p>');
$('#result').append('<p>'+data[key]['paymentTestId']+'</p>');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="result"></div>
$(document).ready(function(){
$.get("ajax_call.php", function(data){
//console.log(data);
var result = JSON.parse(data);
$.each(result, function(key, value){
$('#data').append('<tr><td>'+result[key]['username']+'</td><td>'+result[key]['email']+'</td><td>'+result[key]['mobile']+'</td></tr>');
console.log(result[key])
});
});
});