json字符串,JSONObject对象,JavaBean对象互转。
来源:互联网 发布:banner淘宝设计素材 编辑:程序博客网 时间:2024/06/07 23:48
1.json字符串转为JSONObject对象:
String jsonStr = "{\"name\":\"1\",\"age\":1,\"id\":0}";// json字符串转为JSONObject 对象JSONObject jsonObject = JSONObject.fromObject(jsonStr);System.out.println("name:" + jsonObject.get("name"));System.out.println("age:" + jsonObject.get("age"));System.out.println("id:" + jsonObject.get("id"));
2.JSONObject转为json字符串:
// JSONObject转为json字符串String string = jsonObject.toString();System.out.println("string" + string);
javaBean对象如下:
package pojo;public class People {private int id;private int age;private String name;public People(int id, int age, String name) {super();this.id = id;this.age = age;this.name = name;}public People() {super();}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public String getName() {return name;}public void setAge(int age) {this.age = age;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "People [id=" + id + ", age=" + age + ", name=" + name + "]";}}
3.JSONObject对象转化为javaBean对象:// JSONObject转为javaBeanPeople people = (People)JSONObject.toBean(jsonObject, People.class);System.out.println(people);4.javaBean对象转为 JSONObject对象:
// javaBean转为JSONObject对象People javaBean = new People();javaBean.setAge(100);javaBean.setId(1);javaBean.setName("samllking");JSONObject peopleJson = JSONObject.fromObject(javaBean);String string2 = peopleJson.toString();System.out.println("String2 : " + string2);5.通常更多的时候,我们需要将一个集合json转化为一个java的List<T>集合:例如如下的一个json字符串:
[{"age":1,"id":1,"name":"first"},{"age":2,"id":2,"name":"second"},{"age":3,"id":3,"name":"third"}]这个json字符串中包含的数据可以转化为一个List<People>集合,代码如下:// 将List的Json字符串转化为List<T>集合String listStr = "[{\"age\":1,\"id\":1,\"name\":\"first\"},{\"age\":2,\"id\":2,\"name\":\"second\"},{\"age\":3,\"id\":3,\"name\":\"third\"}]";JSONArray jsonArray2 = JSONArray.fromObject(listStr);List<People> peopleList2 = (List<People>)JSONArray.toCollection(jsonArray2, People.class);for(People peo : peopleList2){System.out.println(peo);}
6.JSONArray的遍历:List<People> peopleList = new ArrayList<People>();peopleList.add(new People(1,1,"first"));peopleList.add(new People(2,2,"second"));peopleList.add(new People(3,3,"third"));JSONArray jsonArray = JSONArray.fromObject(peopleList);// JSONArray的遍历for (int i = 0; i < jsonArray.size(); i++) {JSONObject jsonObject2 = jsonArray.getJSONObject(i);People people1 = (People)jsonObject.toBean(jsonObject2, People.class);System.out.println(people1);}
总结:当然这里面Json字符串转为javaBean的步骤底层肯定是通过java的反射,用一定的字符串匹配规则调用javaBean的Getter和Setter方法,这里面还有一些坑,以后单独写一篇详解。
0 0
- json字符串,JSONObject对象,JavaBean对象互转。
- json字符串与JavaBean对象互转
- 使用Java JSONObject操作JSON字符串对象
- JSON字符串转化为JSONOBJECT对象
- jquery转换json对象为字符串JSON.stringify(jsonObject)
- 对象转化为json格式字符串,json格式字符串转化为JSONObject对象,取出JSONObject对象里的key值
- org.json.JSONObject, 字符串中嵌套着对象
- json字符串 与jsonObject对象的相互转换
- JSONObject 转换 JSON复杂对象
- 使用JSONObject对JSON字符串转对象时重复value的结果
- web服务端使用JSONArrayy或JSONObject转json字符串为Java对象或数组
- Json字符串与Json对象互转
- Json字符串与Json对象互转
- Json对象与Json字符串互转
- Json对象与Json字符串互转
- Json对象与Json字符串互转
- JSON对象与JSON字符串互转
- Json对象与Json字符串互转
- Java锁的膨胀过程和优化
- eclipse报错问题解决
- map容器插入数据之 map[] 和 insert 方式的分析
- P1109 学生分组
- Java只读集合实例
- json字符串,JSONObject对象,JavaBean对象互转。
- 1.0第1章【基础知识】
- Spring源码分析之Bean的加载流程
- Codeforces Round #411 (Div. 1) C. Ice cream coloring
- C#153课的主要内容
- beautifulsoup 解析网页内容
- Mac终端sed 换行-删除空格-删除空行
- 【CS231n笔记】07 Convolutional Neural Networks
- java能不能自己写一个类叫java.lang.System/String正确答案