【字符是json格式】 如何拿取里面的id?

【字符是json格式】 如何拿取里面的id?,第1张

【字符是json格式】 如何拿取里面的id?

目录

前言:

前端获取:

 Java获取:


前言:

        更新一波....... 马上要到月底啦  加油 

群友提问回答 

 array.tostring 如何变成json 获取id....

前端获取:

var str = "[{id:'ywglzhgl_account_system_0',text:'BASS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_543',text:'BOMC',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_2',text:'BOSS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_3',text:'CRM',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_4',text:'P-BOSS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_5',text:'u5176u4ED6u7CFBu7EDF',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_6',text:'BOSSu6D4Bu8BD5u7CFBu7EDF',isLeaf: true,_click: true}]"           如何把所有的id元素给提取出来呢?

var str = "[{id:'ywglzhgl_account_system_0',text:'BASS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_543',text:'BOMC',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_2',text:'BOSS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_3',text:'CRM',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_4',text:'P-BOSS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_5',text:'u5176u4ED6u7CFBu7EDF',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_6',text:'BOSSu6D4Bu8BD5u7CFBu7EDF',isLeaf: true,_click: true}]";   
  var json1=eval("("+str+")"); 
  var h2="";
  json1.forEach((item,index,array)=>{
      //执行代码
   h2+=item.id+";n"
  })
  console.log(h2)

eval("("+str+")");   

 Java获取:

String str ="[{id:'ywglzhgl_account_system_0',text:'BASS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_543',text:'BOMC',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_2',text:'BOSS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_3',text:'CRM',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_4',text:'P-BOSS',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_5',text:'u5176u4ED6u7CFBu7EDF',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_6',text:'BOSSu6D4Bu8BD5u7CFBu7EDF',isLeaf: true,_click: true}]";

String[] ids = str.split("id");

ArrayList list = new ArrayList<>();
for (int i = 1; i < ids.length; i++) {
    list.add(ids[i].substring(ids[i].indexOf("'", 0)+1, ids[i].indexOf("'", 2)));
}


Arrays.stream(ids).forEach(i->{
    System.out.println(i);
});
list.stream().forEach(i->{
    System.out.println(i);
});

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5703367.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存