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. C++ 通过WIN32 API 获取逻辑磁盘详细信息

    众所周知,在微软的操作系统下编写应用程序,最主要的还是通过windows所提供的api函数来实现各种操作的,这些函数通常是可以直接使用的,只要包含windows.h这个头文件, 下载源文件 今天我们主 ...

  2. codeforces A. Table 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/A 题目意思:给出一个n行m列的table,你需要选择一个good cell(假设为(x, y), ...

  3. js将map转成数组

    //根据资源的ID去查找 this.classArray = []; for(var c in this.comboData.classId){ this.classArray.push({ text ...

  4. Thread源码分析

    本文为转载,请珍惜别人的劳动成果,注明转载地址:http://www.cnblogs.com/gw811/archive/2012/10/15/2724602.html 1.Runnable接口源码: ...

  5. Power Strings(poj 2406)

    题意:求字符串中循环节出现的次数 KMP!!! #include<cstdio> #include<iostream> #include<cstring> #def ...

  6. jQuery工具函数

    要点:1.字符串操作2.数组和对象操作3.测试操作4.URL 操作5.浏览器检测6.其他操作 工具函数是指直接依附于 jQuery 对象,针对 jQuery 对象本身定义的方法,即全局性的函数.它的作 ...

  7. unsatisfied类型的异常

    一般为libs包下arm64-v8a,armeabi,armeabi-v7a,mips,mips64,x86,x86_64等文件夹下的.so文件丢失. 一般情况,armeabi下的so文件需要拷贝一份 ...

  8. C++里的静态成员函数不能用const的原因

    From http://blog.csdn.net/freeboy1015/article/details/7634950     static在c++中的第五种含义:用static修饰不访问非静态数 ...

  9. php 工厂模式

    <body> <?php //设计模式:工厂模式 /* class YunSuan { public $a; public $b; function Jia() { return ( ...

  10. Android悬浮窗注意事项

    一 动画无法运行 有时候,我们对添加的悬浮窗口,做动画的时候,始终无法运行. 那么,这个时候,我们可以对要做动画的View,再添加一个parent,即容器.将要做动画的View放入容器中. 二 悬浮窗 ...