jquery json with arrays with numerical keys -


if have php file outputs json data numerical keys, like

<?php $array[1] = "abcd"; $array[2] = "efgh"; $array[3] = "1234"; $array[4] = "5678";  echo json_encode($array); ?> 

how access value key 4? integer in "data.4" below breaking code. appreciated. thanks!

    $.ajax({          type: "get",          url: "http://localhost:8888/myapp/json/json_data",          async: false,          beforesend: function(x) {           if(x && x.overridemimetype) {            x.overridemimetype("application/j-son;charset=utf-8");           }      },      datatype: "json",      success: function(data){         //$("#box").html(json.stringify(data, null, 4));         $("#box").append("<br/>" + data.4)      }     }); 

use brackets access property: data['4'].

note: php not returning array, object: {"1":"abcd","2":"efgh","3":"1234","4":"5678"}.

ps. you've got typo in overridemimetype. shouldn't have override though, because you're using jquery. alternative, see $.getjson.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -