(转)JSONObject的toBean 和 fromObject
public static void main(String[] args) {
Map map=new HashMap();
map.put("我","妹");
map.put("擦","哇");
map.put("你","呀");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
}
輸出的結果 {"我":"妹","擦":"哇","你":"呀"}
toBean();
首先一个javabean对象
public class Student {
private int id ;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.id + ", " + this.name + ", " + this.age;
}
}
然后测试toBean方法的类
import net.sf.json.JSONObject;
public class ToBeanTest {
public static void main(String[] args) {
String json = "{id:'1001',name:'张三',age:'22'}";
Student stu = new Student();
JSONObject obj = JSONObject.fromObject(json);
stu = (Student)JSONObject.toBean(obj, Student.class);
System.out.println(stu);
}
}
输出结果为1001, 张三, 22
然后我们在修改修改
import net.sf.json.JSONObject;
public class ToBeanTest {
public static void main(String[] args) {
String json = "{id:'1001',name:'张三'}";
Student stu = new Student();
JSONObject obj = JSONObject.fromObject(json);
stu = (Student)JSONObject.toBean(obj, Student.class);
System.out.println(stu);
}
}
把年龄给去掉age为int型,输出结果为:1001, 张三, 0
然后再做小小改动
import net.sf.json.JSONObject;
public class ToBeanTest {
public static void main(String[] args) {
String json = "{id:'1001',age:'22'}";
Student stu = new Student();
JSONObject obj = JSONObject.fromObject(json);
stu = (Student)JSONObject.toBean(obj, Student.class);
System.out.println(stu);
}
}
把姓名给去掉name为String型,输出结果为:1001, null, 22
再改动一下:
import net.sf.json.JSONObject;
public class ToBeanTest {
public static void main(String[] args) {
String json = "{id:'1001',name:'张三',age:'nn'}";
Student stu = new Student();
JSONObject obj = JSONObject.fromObject(json);
stu = (Student)JSONObject.toBean(obj, Student.class);
System.out.println(stu);
}
}
把age改成非整形,输出结果为:
1001, 张三, 0
再改动一下:
import net.sf.json.JSONObject;
public class ToBeanTest {
public static void main(String[] args) {
String json = "{id:'1001',name:'张三',age:'22',sex:'男'}";
Student stu = new Student();
JSONObject obj = JSONObject.fromObject(json);
stu = (Student)JSONObject.toBean(obj, Student.class);
System.out.println(stu);
}
}
加了一个sex:'男'的一对键值,输出结果为:
1001, 张三, 22
(转)JSONObject的toBean 和 fromObject的更多相关文章
- JSONObject的toBean 和 fromObject
public static void main(String[] args) { Map map=new HashMap(); map.put("我","妹") ...
- JSONObject与JSONArray
最近在学习过程中用到了稍微复杂点的json数据需要将json数据解析出来,这里就截取一部分作为例子 1.JSONObject介绍 JSONObject-lib包是一个beans,collections ...
- java中JSONObject与JSONArray的使用
JSONObject与JSONArray 最近在学习过程中用到了稍微复杂点的json数据需要将json数据解析出来,这里就截取一部分作为例子 1.JSONObject介绍 JSONObject-lib ...
- JsonObject、JsonArray操作json的个人总结
介绍 JsonObject.JsonArray之前,先介绍下JsonConfig JsonConfig: setClassMap(Map classMap)设置json属性类型,上json里的其中值为 ...
- Json知识总结
JSON对象是一个无序的"名称/值"对的集合它开始于“{”,结束于“}”.每一个属性名和值间用“:”提示,属性间用“,”分隔.一个数组开始于"[",结束于&qu ...
- JAVA中,JSON MAP LIST的相互转换
1 JSON包含对象和数组,对应于JAVA中的JSONObject,JSONArray 2 String 转JSON对象 JSONObject.fromObject("String" ...
- Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)
首先声明一下,jsonlib转换与GSON相比太差劲了,操作不是一般的繁琐.GSON可以直接转换成各种集合与对象类型.强烈推荐使用GSON.而且GSON一个方法就可以解决,jsonlib转来转去太繁琐 ...
- json对象和java对象的相互转换方法(json-lib、jackson、fastjson、gson)
一 使用json-lib包下的JSONObject和JSONArray转换 代码如下: package com.test.test; import java.util.ArrayList; impor ...
- json数据转化成实体 存到数据库.
直接看步骤吧 1.一般我们会调用别人给的webservice获取一个字符串数据.如果为String data="xxxxxxxxxx"; 这个data事实上就是样例Enterpr ...
随机推荐
- php文件操作(最后进行文件常用函数封装)
文件信息相关API $filename="./1-file.php"; //filetype($filename):获取文件的类型,返回的是文件的类型 echo '文件类型为:', ...
- jQuery---固定导航栏案例
固定导航栏案例 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...
- 0004 工程配置settings.py
两个目录的区别: 工程目录是指包含manage.py文件的目录 配置目录是批包含settings.py文件的目录 在配置目录中找到并打工settings.py文件,做以下配置: 01 DEBUG DE ...
- gitlab 更换服务器后访问 Integrations 出现 500 错误
异常问题解决方案:问题:gitlab 更换服务器后访问 Integrations 出现 500 错误解决方案:从原服务器上将 /etc/gitlab/gitlab-secrets.json 复制过来覆 ...
- mybatis-plus - insert
一. insert 首先看一下 insert.java 的代码: /** * <p> * 根据 ID 删除 * </p> * * @author hubin * @since ...
- Jmeter-基础实战
一.测试需求:测试20个用户访问web网站在负载达到30QPS时的平均响应时间 QPS:Query Per Second 每秒查询率.是一台查询服务器每秒能够处理的查询次数.在因特网上,作为域名系统服 ...
- python之路模块补充
=======================================json序列化========================================= ============ ...
- AcWing 1022. 宠物小精灵之收服 二维费用背包
#include<iostream> using namespace std ; ; int f[N][N]; int V1,V2,n; int main() { cin>>V ...
- Mahmoud and Ehab and the message
Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from ...
- 小匠第二周期打卡笔记-Task05
一.卷积神经网络基础 知识点记录: 神经网络的基础概念主要是:卷积层.池化层,并解释填充.步幅.输入通道和输出通道之含义. 二维卷积层: 常用于处理图像数据,将输入和卷积核做互相关运算,并加上一个标量 ...