fastjson解析任意json到bean

解析案例的代码
package com.base.config;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; public class JsonParse { public static void main(String[] args) { String arrJson = "[{\"id\":1,\"mobile\":15809619172},{\"id\":2,\"mobile\":14588827746}]";
List<Student> stus = JSONArray.parseArray(arrJson, Student.class); System.out.println("array json的解析结果:"); for (Student stu : stus) {
System.out.println(stu);
} String beanJson = "{\"id\":1,\"mobile\":15809619172}";
Student stu = JSONObject.parseObject(beanJson, Student.class);
System.out.println("beanjson的解析结果:");
System.out.println(stu); } }
Student的代码
package com.base.config;

public class Student {
private int id;
private String mobile; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
} @Override
public String toString() {
return "Student [id=" + id + ", mobile=" + mobile + "]";
} }
程序运行结果
array json的解析结果:
Student [id=1, mobile=15809619172]
Student [id=2, mobile=14588827746]
beanjson的解析结果:
Student [id=1, mobile=15809619172]

这样解析效率很高,而且代码非常简单。比json-lib库要简化非常多。所以强烈推荐使用fastjson.

fastjson解析任意json的更多相关文章

  1. JSON 之FastJson解析

    http://blog.sina.com.cn/s/blog_7ffb8dd501013qas.html 一.阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具 ...

  2. (转)JSON 之FastJson解析

    一.阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parse ...

  3. Json,Gson,FastJson解析笔记

    Json,Gson,FastJson解析笔记 1.将JavaBean转换成Json对象: public static String CreatJsonFromObject(Object key,Obj ...

  4. fastJson解析复杂的json字符串,经测试已经成功解析

    要解析的json数据格式为: HTTP/1.1 200 OK Content-Type: text/jsv Content-Length: length { ResponseStatus: { }, ...

  5. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  6. fastjson解析json数组

    1.fastjson解析json数组(直接上代码) import java.util.ArrayList; import java.util.List; import com.alibaba.fast ...

  7. JavaWeb_(Jar)使用fastjson解析json和序列化对象

    菜鸟教程 传送门 JSON官网 传送门 fastjson插件下载 传送门 序列化[百度百科]:序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对 ...

  8. FastJson解析Json,封装JavaBean对象

    获取到前端的Json,后台对应封装JavaBean对象,对其解析赋值 获取到前端的json,对其进行分析 1.获取最外层前端json对应得JavaBean (1)未分析格式的json串 (2)初步格式 ...

  9. Scala中使用fastJson 解析json字符串

    Scala中使用fastJson 解析json字符串 添加依赖 2.解析json字符 2.1可以通过JSON中的parseObject方法,把json字符转转换为一个JSONObject对象 2.2然 ...

随机推荐

  1. Java如何读取XML文件 具体实现

    转载自:http://www.jb51.net/article/44338.htm import java.io.*; import javax.xml.parsers.DocumentBuilder ...

  2. 【python】捕获所有异常

    如下所示,在不知道异常名的情况下可以捕获所有异常 try: a=b b=c except Exception,ex: print Exception,":",ex

  3. JSTL分类查询

    index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  4. [MAC] Mac OS X下快速复制文件路径的方法

    在windows上复制当前目录的路径有一个特别方便的方式,只需要用鼠标点击路径栏,它就会自动变成像”D:\Downloads\tmp”这样的路径,如果要复制文件路径,只需要将目录路径和文件名拼接起来即 ...

  5. OC内存管理(ARC)

    1.什么是ARC Automatic Reference Counting,自动引用计数,即ARC,可以说是WWDC2011和iOS5所引入 的最大的变革和最激动人心的变化.ARC是新的LLVM 3. ...

  6. xtrabackup 增量备份(InnoDB)

    mysql> select * from users; +----+-----------+----------+--------------+ | id | name | password | ...

  7. zabbix 修改管理员用户密码

    mysql> use zabbix mysql> desc users; +----------------+---------------------+------+-----+---- ...

  8. hdu 2199:Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. poj 2236 并查集

    并查集水题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...

  10. 如何使用SAE的Storage

    转自:http://blog.csdn.net/xujainxing/article/details/8981904 Storage在里面当然可以创建文件夹,只不过无法通过代码创建,而是在后台管理页面 ...