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对象之间互相转换的更多相关文章

  1. <摘录>Gson对Java嵌套对象和JSON字符串之间的转换

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识, ...

  2. 使用Google的Gson实现对象和json字符串之间的转换

    使用Google的Gson实现对象和json字符串之间的转换 需要gson.jar 1.JsonUtil.java package com.snail.json; import java.lang.r ...

  3. json对象和json字符串之间的转换-JavaScript实现

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. JSON对象与JSON字符串之间的转换

    JSON引用包:import net.sf.json 1.JSON字符串转JSON对象(例子中的AgencyExpand是java对象) JSONObject  json = JSONObject.f ...

  5. Json对象和Json字符串之间的转换

    json字符串转json对象,调用parse方法: var b='{"name":"2323","sex":"afasdf&quo ...

  6. node.js JS对象和JSON字符串之间的转换

    JSON.stringify(obj)将JS对象转为字符串. var json = { aa: ['sdddssd'],   bb: [ '892394829342394792399', '23894 ...

  7. 前端页面使用 Json对象与Json字符串之间的互相转换

    前言 在前端页面很多时候都会用到Json这种格式的数据,最近没有前端,后端的我也要什么都要搞,对于Json对象与Json字符串之间的转换终于摸清楚了几种方式,归纳如下! 一:Json对象转换为json ...

  8. 为javasript中对象与json串之间转换的总结。

    jQuery.json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换.可序列化 JavaScript 对象.数值.字符串和数组到 JSON 字符串,同时可转换 JSON ...

  9. json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值

    一.json相关概念 json,全称为javascript object notation,是一种轻量级的数据交互格式.采用完全独立于语言的文本格式,是一种理想的数据交换格式. 同时,json是jav ...

随机推荐

  1. css的三大特性

    1.继承性 作用:子元素可以继承父元素的样式text-,font-,line-这些元素开头的都可以继承,以及color属性特殊性: ①. a标签的颜色不能继承,必须对a标签本身进行设置 ②. h标签的 ...

  2. Angular之Providers (Value, Factory, Service and Constant )

    官方文档Providers Each web application you build is composed of objects that collaborate to get stuff do ...

  3. 为什么PHP(CLI)同一个错误信息会打印两次?

    第一个信息是display_errors输出的,在fpm环境下输出到浏览器那里,而在CLI环境下会打印到屏幕上. display_errors = On 第二个信息是log_errors输出的. lo ...

  4. 深耕教育行业,RealSeer联合黑晶科技发布“AR超级教室”

    近日,RealSeer开发者大赛见面会最后一站在北京举行,现场云集了不少AR创业者和开发者,各位大咖嘉宾都拿出干货与大家分享交流,公话未来AR行业发展趋势.现场RealMax联合黑晶科技发布了新品&q ...

  5. Node.js:url

    在node运行环境中输入url: > url url { parse: [Function: urlParse], resolve: [Function: urlResolve], resolv ...

  6. 【LeetCode】434. Number of Segments in a String

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  7. php基础(五)日期

    PHP Date() 函数 date(format,timestamp) 参数 描述 format 必需.规定时间戳的格式. timestamp 可选.规定时间戳.默认是当前时间和日期. 注释:时间戳 ...

  8. Haskell开始

    一.安装 操作系统为centos,为了安装最新的Haskell编译器,不使用命令 yum install ghc 1.安装ghc 使用wget下载ghc,命令如下(注意系统是centos6.7) $ ...

  9. .Net使用JsonSchema验证Json

    最近项目中遇到了这样的需求,需要对上传的Json进行验证,以确保Json数据的准确性.前后使用了两种方式来验证: (1)第一种方式的实现思想:根据Json数据的格式,严格定义相应的类结构,并在Syst ...

  10. stack(STL)

    //Stack STL //在STL中,栈是以别的容器作为底部结构,再将 //接口改变,使之符合栈的特性 //一共5个常用操作函数 //构造析构 stack<Elem>c; //build ...