将Json编码数据转换为javascript数组并按索引访问值

时间:2017-04-06 12:54:56

标签: javascript jquery json ajax-request

我从像这样的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

我将如何实现这一目标?

注意:我在stackoverflow上尝试过很多例子,我知道这个问题必须先回答。但我仍然被卡住了。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

你得到的是一串编码的JSON,用它作为你必须解析它的对象。

Check this answer

答案 1 :(得分:1)



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="result"></div>
&#13;
function ver(fotox, texto1) {
  var collection = document.getElementsByClassName("link")
  var array = Array.prototype.slice.call(collection, 0 );

  array.forEach(function(currentValue, index, array) {
    currentValue.className = 'link';
  });

  var x = document.getElementById(fotox);
  x.className = 'link active';
  x.innerHTML = texto1;
}
&#13;
&#13;
&#13;

答案 2 :(得分:1)

$(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])

        });

    });

});    

答案 3 :(得分:0)

const DynamicMenuExample = (props) => (
  <div>
    <ContextMenuTrigger id={MENU_ID} itemLabel='one'
        connect={props => props}>
      {'Click me for a menu with a "one" item.'}
    </ContextMenuTrigger>
    <ContextMenuTrigger id={MENU_ID}
        connect={() => ({itemLabel: 'other'})}>
      {'Click me for a menu with an "other" item.'}
    </ContextMenuTrigger>
    <ContextMenuTrigger id={MENU_ID} itemLabel='third'
        connect={props => ({ itemLabel: props.itemLabel})}>
      {'Click me for a menu with a "third" item.'}
    </ContextMenuTrigger>
    <ConnectedMenu />
  </div>
);