获取JsonObject某一未知key的值操作
作者:辰辰呐 时间:2023-02-10 15:09:45
我就废话不多说了,大家还是直接看代码吧~
//returnContent为获取到的返回参数
System.out.println(returnContent);
//参数转换成json格式
JSONObject jsonObject = JSON.parseObject(returnContent);
//获取json里面的值key(可以省略)
String ErrMsg = jsonObject.getString("ErrMsg");
String passwords = jsonObject.getString("passwords");
//即将要提取的jsonObject的字符串转换为json格式
JSONObject jsonpwd = JSON.parseObject(passwords);
//正式提取未知的key值
Iterator<String> sIterator = jsonpwd.keySet().iterator();
//循环并得到key列表
while (sIterator.hasNext()) {
// 获得key
String key = sIterator.next();
//获得key值对应的value
String value = jsonpwd.getString(key);
JSONObject jsonvalue = JSON.parseObject(value);
String id = jsonvalue.getString("id");
System.out.println("id: " + id );
}
补充:java 解析json 遍历未知key
看代码吧~
import net.sf.json.JSONObject;
String json = "{\"name\":\"lss\"}";
JSONObject jsonObj = JSONObject.fromObject(json);
String name = jsonObj.getString("name");
Iterator it = jsonObj.keys();
List<String> keyListstr = new ArrayList<String>();
while(it.hasNext()){
keyListstr.add(it.next().toString());
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_37581708/article/details/72419707
标签:JsonObject,key
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot 入门教程
2023-05-26 00:14:44
Android开发之Button事件实现与监听方法总结
2022-02-05 02:45:40
C#调用WebService的方法介绍
2022-06-05 01:10:30
![](https://img.aspxhome.com/file/2023/0/89850_0s.jpg)
Java Swing编写一个简单的计算器软件
2023-08-12 06:24:04
![](https://img.aspxhome.com/file/2023/3/57953_0s.png)
java多线程之铁路售票系统
2022-01-31 12:25:14
Java实现把两个数组合并为一个的方法总结
2022-08-04 16:30:03
springboot中.yml文件参数的读取方式
2021-06-20 00:57:51
![](https://img.aspxhome.com/file/2023/3/62413_0s.png)
C#中实现Json序列化与反序列化的几种方式
2021-11-06 01:31:07
![](https://img.aspxhome.com/file/2023/5/71065_0s.png)
SpringBoot实现监控Actuator,关闭redis监测
2021-08-01 04:01:33
![](https://img.aspxhome.com/file/2023/6/130006_0s.jpg)
Android通过HTTP协议实现断点续传下载实例
2022-12-05 23:39:05
解决Java中OutOfMemoryError的问题
2023-01-26 13:51:03
![](https://img.aspxhome.com/file/2023/8/76498_0s.png)
Android自定义View控件实现刷新效果
2022-05-12 14:43:22
![](https://img.aspxhome.com/file/2023/7/138157_0s.png)
java高并发写入用户信息到数据库的几种方法
2023-07-09 08:45:49
Spring集成Druid连接池及监控配置的全过程
2023-08-25 05:22:20
![](https://img.aspxhome.com/file/2023/8/63448_0s.png)
Android RecyclerView添加上拉加载更多功能
2022-08-02 00:03:51
![](https://img.aspxhome.com/file/2023/8/94288_0s.gif)
Android在线更新SDK的方法(使用国内镜像)
2022-08-31 02:40:31
![](https://img.aspxhome.com/file/2023/1/138811_0s.png)
Spring源码完美导入IDEA的过程
2023-05-13 14:11:50
![](https://img.aspxhome.com/file/2023/3/80323_0s.jpg)
解析MapStruct转换javaBean时出现的诡异事件
2022-05-15 01:16:28
![](https://img.aspxhome.com/file/2023/1/71701_0s.png)
Android 消息机制详解及实例代码
2023-07-28 08:37:49
![](https://img.aspxhome.com/file/2023/3/85963_0s.png)
Android 存储路径选择方法
2022-04-03 21:05:42