douzhi9635 2014-04-11 17:51
浏览 52
已采纳

将json_encode数据转换为javascript数组

I am send some data from php by json_encode to javascript. this is the data

[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]

after enoding in php it is something like that

       //json_encode($data);

"[{\"albumid\":\"ASaBFzCtl8\",\"albumname\":\"anni\",\"type\":\"3\",\"access\":\"2\",\"itemcount\":\"2\"},{\"albumid\":\"EmgsZ43ehT\",\"albumname\":\"testalbum\",\"type\":\"1\",\"access\":\"1\",\"itemcount\":\"0\"},{\"albumid\":\"Jf4H4SvFGk\",\"albumname\":\"test2album\",\"type\":\"3\",\"access\":\"1\",\"itemcount\":\"0\"},{\"albumid\":\"k3pacBSmIl\",\"albumname\":\"testalbumpvt\",\"type\":\"3\",\"access\":\"2\",\"itemcount\":\"0\"}]"

i am receiving this in jquery

$.post("demo.php",
  {
    token:"123456789"
  },
  function(data,status){
    alert("Data: " + data + "
Status: " + status);
  });

what can i do so that i get all data in javascript array i need type value out of it and looking something like that

var typevalue = jsonArray['type'];
// typevalue = 3
  • 写回答

4条回答 默认 最新

  • dongshi7433 2014-04-11 17:58
    关注

    Assuming you have parsed json here's how you loop it:

    $.each(data, function(key, album){
        console.log(album.type);
    });
    

    To grab the first type in the list simply do:

    var albumType = data[0].type;
    

    Full solution with parsing:

    $.post("demo.php", {
        token: "123456789"
    },
    
    function (data, status) {
        $.each(data, function (key, album) {
            alert(album.type);
        });
    
    }, "json"); //datatype defined here
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度