首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
编程语言
> 详细
java对象和json对象之间互相转换
时间:
2015-08-28 17:25:56
阅读:
123
评论:
0
收藏:
0
[点我收藏+]
标签:
[java]
view plain
copy
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public
class MainClass {
public
static
void main(String[] args) {
TestJsonBean();
TestJsonAttribute();
TestJsonArray();
}
@SuppressWarnings(
"rawtypes")
private
static
void TestJsonArray() {
Student student1 =
new Student();
student1.setId(
1);
student1.setName(
"jag");
student1.setSex(
"man");
student1.setAge(
25);
student1.setHobby(
new String[]{
"篮球",
"游戏"});
Student student2 =
new Student();
student2.setId(
2);
student2.setName(
"tom");
student2.setSex(
"woman");
student2.setAge(
23);
student2.setHobby(
new String[]{
"上网",
"跑步"});
List<Student> list =
new ArrayList<Student>();
list.add(student1);
list.add(student2);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString());
JSONArray new_jsonArray=JSONArray.fromObject(jsonArray.toArray());
Collection java_collection=JSONArray.toCollection(new_jsonArray);
if(java_collection!=
null && !java_collection.isEmpty())
{
Iterator it=java_collection.iterator();
while(it.hasNext())
{
JSONObject jsonObj=JSONObject.fromObject(it.next());
Student stu=(Student) JSONObject.toBean(jsonObj,Student.
class);
System.out.println(stu.getName());
}
}
}
private
static
void TestJsonAttribute() {
/**
* 创建json对象并为该对象设置属性
*/
JSONObject jsonObj =
new JSONObject();
jsonObj.put(
"Int_att",
25);
//添加int型属性
jsonObj.put(
"String_att",
"str");
//添加string型属性
jsonObj.put(
"Double_att",
12.25);
//添加double型属性
jsonObj.put(
"Boolean_att",
true);
//添加boolean型属性
//添加JSONObject型属性
JSONObject jsonObjSon =
new JSONObject();
jsonObjSon.put(
"id",
1);
jsonObjSon.put(
"name",
"tom");
jsonObj.put(
"JSONObject_att",jsonObjSon);
//添加JSONArray型属性
JSONArray jsonArray =
new JSONArray();
jsonArray.add(
"array0");
jsonArray.add(
"array1");
jsonArray.add(
"array2");
jsonArray.add(
"array3");
jsonObj.put(
"JSONArray_att", jsonArray);
System.out.println(jsonObj.toString());
System.out.println(
"Int_att:"+jsonObj.getInt(
"Int_att"));
System.out.println(
"String_att:"+jsonObj.getString(
"String_att"));
System.out.println(
"Double_att:"+jsonObj.getDouble(
"Double_att"));
System.out.println(
"Boolean_att:"+jsonObj.getBoolean(
"Boolean_att"));
System.out.println(
"JSONObject_att:"+jsonObj.getJSONObject(
"JSONObject_att"));
System.out.println(
"JSONArray_att:"+jsonObj.getJSONArray(
"JSONArray_att"));
}
/**
* java对象与json对象互相转换
*/
private
static
void TestJsonBean() {
/**
* 创建java对象
*/
Student student =
new Student();
student.setId(
1);
student.setName(
"jag");
student.setSex(
"man");
student.setAge(
25);
student.setHobby(
new String[]{
"篮球",
"上网",
"跑步",
"游戏"});
/**
* java对象转换成json对象,并获取json对象属性
*/
JSONObject jsonStu = JSONObject.fromObject(student);
System.out.println(jsonStu.toString());
System.out.println(jsonStu.getJSONArray(
"hobby"));
/**
* json对象转换成java对象,并获取java对象属性
*/
Student stu = (Student) JSONObject.toBean(jsonStu, Student.
class);
System.out.println(stu.getName());
/**
* 创建json对象
*/
JSONObject jsonObj =
new JSONObject();
jsonObj.put(
"id",
1);
jsonObj.put(
"name",
"张勇");
jsonObj.put(
"sex",
"男");
jsonObj.put(
"age",
24);
//jsonObj.put("hobby",new String[]{"上网","游戏","跑步","音乐"});
//System.out.println(jsonObj.toString());
/**
* json对象转换成java对象
*/
Student stud = (Student) JSONObject.toBean(jsonObj,Student.
class);
System.out.println(stud.getName());
}
}
java对象和json对象之间互相转换
标签:
原文地址:http://www.cnblogs.com/zhwl/p/4766814.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
Spring Cloud 从入门到精通(一)Nacos 服务中心初探
2021-07-29
基础的排序算法
2021-07-29
SpringBoot|常用配置介绍
2021-07-29
关于 .NET 与 JAVA 在 JIT 编译上的一些差异
2021-07-29
C语言常用函数-toupper()将字符转换为大写英文字母函数
2021-07-29
《手把手教你》系列技巧篇(十)-java+ selenium自动化测试-元素定位大法之By class name(详细教程)
2021-07-28
4-1 YAML配置文件 注入 JavaBean中
2021-07-28
【python】 用来将对象持久化的 pickle 模块
2021-07-28
马拉车算法
2021-07-28
用Python进行冒泡排序
2021-07-28
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!