java对象和json对象之间互相转换
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对象之间互相转换的更多相关文章
- <摘录>Gson对Java嵌套对象和JSON字符串之间的转换
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识, ...
- 使用Google的Gson实现对象和json字符串之间的转换
使用Google的Gson实现对象和json字符串之间的转换 需要gson.jar 1.JsonUtil.java package com.snail.json; import java.lang.r ...
- json对象和json字符串之间的转换-JavaScript实现
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- JSON对象与JSON字符串之间的转换
JSON引用包:import net.sf.json 1.JSON字符串转JSON对象(例子中的AgencyExpand是java对象) JSONObject json = JSONObject.f ...
- Json对象和Json字符串之间的转换
json字符串转json对象,调用parse方法: var b='{"name":"2323","sex":"afasdf&quo ...
- node.js JS对象和JSON字符串之间的转换
JSON.stringify(obj)将JS对象转为字符串. var json = { aa: ['sdddssd'], bb: [ '892394829342394792399', '23894 ...
- 前端页面使用 Json对象与Json字符串之间的互相转换
前言 在前端页面很多时候都会用到Json这种格式的数据,最近没有前端,后端的我也要什么都要搞,对于Json对象与Json字符串之间的转换终于摸清楚了几种方式,归纳如下! 一:Json对象转换为json ...
- 为javasript中对象与json串之间转换的总结。
jQuery.json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换.可序列化 JavaScript 对象.数值.字符串和数组到 JSON 字符串,同时可转换 JSON ...
- json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值
一.json相关概念 json,全称为javascript object notation,是一种轻量级的数据交互格式.采用完全独立于语言的文本格式,是一种理想的数据交换格式. 同时,json是jav ...
随机推荐
- SSH整合例子
三大框架: Struts框架 1. params拦截器: 请求数据封装 2. 类型转换/数据处理 3. struts配置 4. 文件上传/下载/国际化处理 5. 数据效验/拦截器 6. Ognl表达式 ...
- Python朝花夕拾
Q1:HTTP Error 403: Forbidden python中经常使用urllib2.urlopen函数提取网页源码,但是有些时候这个函数返回的却是:HTTP Error 403: Forb ...
- Android app作为系统应用实现功能笔记
1.禁用StatusBar相关功能需要添加权限 <uses-permission android:name="android.permission.STATUS_BAR"&g ...
- Linux的cron和crontab
一 cron crond位于/etc/rc.d/init.d/crond 或 /etc/init.d 或 /etc/rc.d /rc5.d/S90crond,最总引用/var/lock/subsys/ ...
- Swift逃逸闭包之见解
Swift 逃匿闭包顾名思义,就是闭包想要逃跑.当闭包作为参数传给一个方法时,在这个方法被调用完后闭包却还没有被执行,而是等到方法执行完后才调用 基本都是跨线程的时候才会有逃逸闭包这个说法.因为异步 ...
- php 导出 Excel 报错 exception 'PHPExcel_Calculation_Exception' with message
exception 'PHPExcel_Calculation_Exception' with message '粉丝数据!C2679 -> Formula Error: Operator '= ...
- C# 读书笔记之类与结构体
类和结构体都包括数据和操作数据的方法 类的定义形式 class PhoneCustomer{public const string DayOfSendingBill = "Monday&qu ...
- 自己写deque
//deque /* what is a deque? In Chinese, it's called "双端队列". It's different from a queue. I ...
- OMCS使用技巧 -- 摄像头及其动态能力
在开发类似视频聊天的应用时,我们经常需要获取摄像头的相关信息:而在进行视频聊天时,我们可能还希望有一些动态的能力.比如,在不中断视频聊天的情况下,切换一个摄像头.或者修改摄像头采集的分辨率或编码质量等 ...
- UPX3.03+UpolyX.5 Shell v1.0 汉化绿色版
软件名称:UPX3.03+UpolyX.5 Shell v1.0 汉化绿色版软件类别:汉化软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:635 KB软件等级:整理时间:201 ...