【字符是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:'\u5176\u4ED6\u7CFB\u7EDF',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_6',text:'BOSS\u6D4B\u8BD5\u7CFB\u7EDF',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:'\u5176\u4ED6\u7CFB\u7EDF',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_6',text:'BOSS\u6D4B\u8BD5\u7CFB\u7EDF',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:'\u5176\u4ED6\u7CFB\u7EDF',isLeaf: true,_click: true},{id:'ywglzhgl_account_system_6',text:'BOSS\u6D4B\u8BD5\u7CFB\u7EDF',isLeaf: true,_click: true}]";

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

ArrayList<String> 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);
});

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Json(JavaScript Object Notation)是一种轻量级的数据交换格式,具有易读易写、轻量级等特点。它的数据格式是键值对的集合,可以包含对象、数组等复杂数据类型。 手动将对象或集合转换成Json字符串,可以使用Json库提供的方法,例如在Java中可以使用Gson库的toJson()方法,将对象或集合转换成Json格式字符串。 示例代码: ```java // 将对象转成Json字符串 Person person = new Person("张三", 20); String jsonStr = new Gson().toJson(person); // 将集合转成Json字符串 List<Person> personList = new ArrayList<>(); personList.add(new Person("张三", 20)); personList.add(new Person("李四", 21)); String jsonStr = new Gson().toJson(personList); ``` 配置Json类型转换,可以根据具体的框架进行配置。例如在Spring框架中,可以通过配置Jackson库的ObjectMapper来实现Json类型的转换。 示例配置: ```xml <bean id="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="featuresToEnable"> <array> <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" /> </array> </property> </bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper" ref="jacksonObjectMapper" /> </bean> </list> </property> </bean> ``` 以上示例代码中,使用Jackson库的ObjectMapper将Java对象转换成Json格式字符串,通过Spring框架的配置,将Json类型转换器配置到RequestMappingHandlerAdapter中,使得Spring MVC可以自动将请求和响应中的Json数据转换成Java对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是汤圆丫

怎么 给1分?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值