fast-json.jar的使用方法

fast-json.jar 解析json数据:一种json数据解析方式是这样的,点击这里下载jsonfast.jar+fastjsonAPI文档

[
    {
        "id": 6378,
        "title": "test",
        "img": "http://image.jxvdy.com/2014/0929/5428d91c9e6dc8f78fd99_0.png",
        "score": 0,
        "description": "test",
        "time": 1411963174
    },
    {
        "id": 6142,
        "title": "微电影多角度拍摄技巧(三)",
        "img": "http://image.jxvdy.com/old/201409/24/11-54-15-17-1531.jpg",
        "score": 0,
        "description": "",
        "time": 1411530850
    },
    {
        "id": 6141,
        "title": "微电影多角度拍摄技巧(一)",
        "img": "http://image.jxvdy.com/old/201409/24/11-54-04-89-1531.jpg",
        "score": 0,
        "description": "",
        "time": 1411530835
    },
    {
        "id": 6140,
        "title": "微电影多角度拍摄技巧(二)",
        "img": "http://image.jxvdy.com/old/201409/24/11-49-54-18-1531.jpg",
        "score": 0,
        "description": "",
        "time": 1411530552
    },
    {
        "id": 4355,
        "title": "施比受,更有福",
        "img": "http://image.jxvdy.com/old/201409/24/11-46-06-65-3.jpg",
        "score": 0,
        "description": "一位老人用自己的一半时间去帮助他人,赠予帮助,收获快乐",
        "time": 1411530082
    },
    {
        "id": 4354,
        "title": "父子时光之旅",
        "img": "http://image.jxvdy.com/old/201409/24/11-35-13-81-3.jpg",
        "score": 0,
        "description": "当父亲老去,忙于生活的男人没有时间照顾体弱的父亲,于是,带上父亲上路吧,带他重走当年他走过无数遍的那段旅程",
        "time": 1411529699
    }
]

对于这一种json数据,使用fastjson进行解析的时候,调用方法之前应该先写出其对应的bean.java(我想你已经做过了);上面的json数据对应的bean是这样的,

public class NewMoviesBean {

	private int id;
	private String title;
	private String img;
	private String score;
	private String description;
	private int time;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getImg() {
		return img;
	}
	public void setImg(String img) {
		this.img = img;
	}
	public String getScore() {
		return score;
	}
	public void setScore(String score) {
		this.score = score;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
	public NewMoviesBean(int id, String title, String img, String score,
			String description, int time) {
		super();
		this.id = id;
		this.title = title;
		this.img = img;
		this.score = score;
		this.description = description;
		this.time = time;
	}
	public NewMoviesBean() {
		super();
	}
	@Override
	public String toString() {
		return "NewMoviesBean [id=" + id + ", title=" + title + ", img=" + img
				+ ", score=" + score + ", description=" + description
				+ ", time=" + time + "]";
	}

}

那么对应的解析方法是这样的:

JSON.parseArray(json, NewMoviesBean.class);
为甚么回事这种解析方式呢?因为,分析整个json数据的格式我们能发现,最外层是中括号"[ ]",内侧是大括号"{ }";中括号说明整个json数据为一个数组类型,其中的大括号说明是数组中的元素;说明整个就是一个JSONArray,JSONArray中元素又是一个个的JSONObject。

另一种的解析方式:json数据是这样的,

{"type": [
        "恐怖",
        "剧情"
    ]},

分析这种形式,大括号里面是小括号。也即是数组整体是通过键值对的形式呈现的。那么最外层就是一个JSONObject,KEY对应的就是JSONArray。应该这样:

JSONArray jsonArrayType = JSONObject.getJSONArray("type");
			String[] type = new String[jsonArrayType.size()];
			for (int j = 0; j < jsonArrayType.size(); j++) {
				type[j] = (String)jsonArrayType.get(j);
			}

这样就能够解析出想要的数据。

与上面类似的另一种解析:json数据是这样的:

{
        "playurl": {
            "360P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGQ",
            "480P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGT",
            "720P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGZ"
        }
    }

这种形式,外层大括号里面是一个键KEY对应了另一个大括号元素,那么其最外层是一个JSONObject;内层KEY对应的也是一个JSONObject。

当然也可以先创建开一个bean:

public class MoviedefinitionBean {

	private String normalP;
	private String hightP;
	private String superP;
	public String getNormalP() {
		return normalP;
	}
	public void setNormalP(String normalP) {
		this.normalP = normalP;
	}
	public String getHightP() {
		return hightP;
	}
	public void setHightP(String hightP) {
		this.hightP = hightP;
	}
	public String getSuperP() {
		return superP;
	}
	public void setSuperP(String superP) {
		this.superP = superP;
	}
	public MoviedefinitionBean(String normalP, String hightP, String superP) {
		super();
		this.normalP = normalP;
		this.hightP = hightP;
		this.superP = superP;
	}
	public MoviedefinitionBean() {
		super();
	}
	@Override
	public String toString() {
		return "MoviedefinitionBean [normalP=" + normalP + ", hightP=" + hightP
				+ ", superP=" + superP + "]";
	}

}

然后对此做出解析:

JSONObject jsonObjectDefination = jsonObject.getJSONObject("playurl");
				String normalP = jsonObjectDefination.getString("360P");
				String hightP = jsonObjectDefination.getString("480P");
				String superP = jsonObjectDefination.getString("720P");
				playurl = new MoviedefinitionBean(normalP, hightP, superP);

今天先写到这里|10-02-2014.

时间: 2024-08-09 16:06:20

fast-json.jar的使用方法的相关文章

Android学习笔记之Fast Json的使用

PS:最近这两天发现了Fast Json 感觉实在是强大.. 学习内容: 1.什么是Fast Json 2.如何使用Fast Json 3.Fast Json的相关原理 4.Fast Json的优势,以及为什么推荐使用Fast Json 1.Fast Json的相关介绍   说道Json想必我们都不陌生,数据传输的两种形式之一,另一种就是我们的xml了.不过现在更多的还是使用基于Json的格式来进行数据传输,Java的api接口为我们提供了相关的api接口用于对Json数据进行处理,如何将数据打

Flink SQL解析Json格式数据的方法

1. Flink版本1.7.2 2. 引入依赖 使用maven构建工程,因此pom.xml添加如下依赖: <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-table_2.11</artifactId> <version>1.7.2</version> </dependency> <!-- https://mvnrepos

SpringBoot 返回json 字符串(jackson 及 fast json)

一.jackson 1.Controller 类加注解@RestController 这个注解相当于@Controller 这个注解加 @ResponseBody 2.springBoot 默认使用 jackson 来把java 对象转化为json 字符串. 二.fast json 1.pom 文件加入fast json 依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson&l

AJAX跨域请求json数据的实现方法

这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式,它是可以引用跨域URL的js脚本,不过需要提供一个回调函数(必须在您自己的页面上),因此,你可以自己处理结果. 让我们看看JSONP的是怎么在jQuery,MooTools的,Dojo Toolkit中实现的. jQuery的JSONPjQuery.getJSON方法:Js代码 jQuery.get

JS:字符串转成json数据,和json转成字符串方法 iframe获取父级传过来的数据

字符串转成json数据,和json转成字符串方法 //转为JSON adinfo=JSON.parse(adinfo) //转为字符串 adinfo=JSON.stringify(adinfo) 大概流程: var gdt_adinfo=[]; 父级页面通过接口获取的数据: this.url="http://gjs.adwo.com/gjs/gad_i?sdkVersion="+sdkVersion+"&n=1&gp="+gp; 获取数据后处理的方法

Win7/Win8下双击运行jar程序的方法

问题 老妈喜欢看小说,又不会下载,于是用Java写了个自动下载小说的小程序 我用的Swing UI,直接生成了.jar文件,双击即可运行 很诡异的是,用Eclipse直接run可以运行,但是Export出jar文件以后双击却没有反应 探究 怀疑是不是Eclipse的导出功能坏掉了,于是用了flatjar等工具重新打包,仍然不行 走了很多弯路以后终于Google到了问题的所在: win7/win8的jar文件默认关联的程序是java.exe而不是javaw.exe 在右键 -> 打开方式里面这两个

JSON数据的序列化方法

ajax传参是json数据对象时,最好是将json对象先序列化 var stuAnswerTotal = examModule.touch.getData('examAnswer'); console.log(stuAnswerTotal);//Object对象如下 { 17072={ "id" : 1702, "type":"1",                "val":["",'"&quo

【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)

原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添加进工作的build path.3.(关键的一步)将lib设为源文件夹.如果不设置,则程序编译可以通过,但运行的时候,会报: java.lang.NoClassDefFoundError # re: Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundErro

打jar包的方法

打jar包的方法是什么? java打jar包,引用其他.jar文件 java项目打jar包 将java源码打成jar包 maven打jar例子 打war包的方法是什么? Eclipse->项目右键=>export导出项目成war包. 打jar包的方法,布布扣,bubuko.com

【转】JMeter中返回Json数据的处理方法

Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Json 数据的响应处理.本文中假设需要从 HTTP 的响应头中返回的 Json 格式的数据流中抽取某些特定的数据,数据格式如下: { "name":"Simpsons family", "members":[ {"firstName"