首页 > 代码库 > Android数据解析-JSON解析

Android数据解析-JSON解析

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,web开发中这个哥们时常出现在你的程序中,你还不得不解析,一般说到JSON,都会不由自主的对比一下它的前辈XML,XML从设计之初到现在已经有十年的光景,在有些方面的作用已经超出其当时设计XML的初衷,WebService是XML的主要的战场,在网络应用场景中由于XML自身的标签形式和Dom形式的数据,解析相对来说会相对麻烦。这个时候JDouglas Crockford提出了JSON,不写不知道,才知道上次买的《JavaScript语言精粹》也是这货写的,⊙﹏⊙~说多了,进入正题吧:

JSON与XML

可读性:JSON和XML的可读性不相上下,JSON格式常用的符号是逗号,小括号,冒号,中括号,引号五种,比较简洁,XML规范的标签形式,都很易读易懂。

可扩展性:XML天生有很好的扩展性,JSON也有,没有什么是XML能扩展,而JSON却不能扩展。不过JSON在Javascript主场作战,可以存储Javascript复合对象,有着xml不可比拟的优势。

编码难度:XML有丰富的编码工具,比如Dom4j、JDom等,JSON也有提供的工具。无工具的情况下,写JSON会比XML稍微快一点(想对我而言),xml文档要多很多结构上的字符。

JSON数据格式:

键值对的形式:{"Name":"FlyElephant"}

 

数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔,例如:[{"Name":"FlyElephant"},{"Name":"中山 郎"}]

 

值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、null、对象(object)或者数组(array)。这些结构可以嵌套.

 

JSON解析

Json的格式是固定的,就意味着大家可以按照规则自己解析,不过为了提高开发效率,有些工具提供了简单的功能供大家使用,比如说json-lib,GSON,既然是搞Android,就使用Gsonj解析,Gson下载地址https://code.google.com/p/google-gson/downloads/list?can=1&q=,下载最新的那个即可~之后添加到自己项目中~

不管是开发还是测试都离不开类对象,所以这个时候先建了一个Book类:

public class Book {	public Book(String bookName, String author) {		super();		BookName = bookName;		Author = author;	}	private String  BookName;	private String  Author;	public String getBookName() {		return BookName;	}	public void setBookName(String bookName) {		BookName = bookName;	}	public String getAuthor() {		return Author;	}	public void setAuthor(String author) {		Author = author;	}}

转换类对象:

	 Book book=new Book("明朝那些事", "当年明月");		 Gson gson=new Gson();		 //生成JSON调用toJSON		 System.out.println("Book: " + gson.toJson(book));		 //解析JSON调用fromJSON		 Book jsonBook=gson.fromJson(gson.toJson(book), Book.class);		 System.out.println("Name:"+jsonBook.getBookName()+"--Author:"+jsonBook.getAuthor());

  结果如下:

Book: {"BookName":"明朝那些事","Author":"当年明月"}Name:明朝那些事--Author:当年明月

  List<String>和类共同使用

		 Gson gson = new Gson();		 List<String> list = new ArrayList<String>();		 list.add("北京");		 list.add("上海");		 list.add("广州");		 list.add("深圳");		 System.out.println("IT城市:" + gson.toJson(list));		//注意这里的第二个参数		 list = gson.fromJson(gson.toJson(list),		 new TypeToken<List<String>>() {		 }.getType());		 for (String string : list) {		 System.out.println(string);		 }						 Book book=new Book(gson.toJson(list), "当年明月");		 System.out.println("Book: " + gson.toJson(book));		 Book jsonBook=gson.fromJson(gson.toJson(book), Book.class);		 System.out.println("Name:"+jsonBook.getBookName()+"--Author:"+jsonBook.getAuthor());

  结果如下:

IT城市:["北京","上海","广州","深圳"]北京上海广州深圳Book: {"BookName":"[\"北京\",\"上海\",\"广州\",\"深圳\"]","Author":"当年明月"}Name:["北京","上海","广州","深圳"]--Author:当年明月

  List<Book>解析:

	Gson gson = new Gson();		List<Book> list = new ArrayList<Book>();		Book book1 = new Book("明朝那些事", "当年明月");		Book book2 = new Book("盗墓笔记", "南派三叔");		Book book3 = new Book("鬼吹灯", "天下霸唱");		list.add(book1);		list.add(book2);		list.add(book3);		System.out.println("Books:" + gson.toJson(list));		List<Book> listJsonBooks = gson.fromJson(gson.toJson(list),				new TypeToken<List<Book>>() {				}.getType());		for (Book book : listJsonBooks) {			System.out.println("Name:" + book.getBookName() + "Author:"					+ book.getAuthor());		}

  结果如下:

Books:[{"BookName":"明朝那些事","Author":"当年明月"},{"BookName":"盗墓笔记","Author":"南派三叔"},{"BookName":"鬼吹灯","Author":"天下霸唱"}]Name:明朝那些事Author:当年明月Name:盗墓笔记Author:南派三叔Name:鬼吹灯Author:天下霸唱

  List<Map<String,String>>解析:

	 Gson gson = new Gson();		 List<Map<String, String>> listMaps = new ArrayList<Map<String,		 String>>();		 Map<String, String> map1 = new HashMap<String, String>();		 map1.put("Name", "FlyElephant");		 map1.put("Address", "上地");		 Map<String, String> map2 = new HashMap<String, String>();		 map2.put("Name", "中山郎");		 map2.put("Address", "北京");		 map2.put("Age", "25");		 listMaps.add(map1);		 listMaps.add(map2);		 System.out.println("Map:"+gson.toJson(listMaps));		 listMaps=gson.fromJson(gson.toJson(listMaps),new		 TypeToken<List<Map<String,String>>>(){}.getType());		 for (Map<String, String> map : listMaps) {		 System.out.println("Name:"+map.get("Name")+"--Address:"+map.get("Address"));		 }

  结果如下:

Map:[{"Address":"上地","Name":"FlyElephant"},{"Address":"北京","Age":"25","Name":"中山郎"}]Name:FlyElephant--Address:上地Name:中山郎--Address:北京

  List<Book>和Map<String>一起解析:

	Gson gson = new Gson();		List<Book> list = new ArrayList<Book>();		Book book1 = new Book("明朝那些事", "当年明月");		Book book2 = new Book("盗墓笔记", "南派三叔");		Book book3 = new Book("鬼吹灯", "天下霸唱");		list.add(book1);		list.add(book2);		list.add(book3);		System.out.println("Books:" + gson.toJson(list));		List<Book> listJsonBooks = gson.fromJson(gson.toJson(list),				new TypeToken<List<Book>>() {				}.getType());		for (Book book : listJsonBooks) {			System.out.println("Name:" + book.getBookName() + "Author:"					+ book.getAuthor());		}		Map<String, String> map = new HashMap<String, String>();		map.put("List", gson.toJson(list));		map.put("Info", "上地");		System.out.println("混合使用:"+gson.toJson(map));		map = gson.fromJson(gson.toJson(map),				new TypeToken<Map<String, String>>() {				}.getType());		System.out.println("List:" + map.get("List") + "--Info:"+map.get("Info"));		 listJsonBooks= gson.fromJson(map.get("List"),				new TypeToken<List<Book>>() {				}.getType());		for (Book book : listJsonBooks) {			System.out.println("Name:" + book.getBookName() + "Author:"					+ book.getAuthor());		}

  结果如下:

Books:[{"BookName":"明朝那些事","Author":"当年明月"},{"BookName":"盗墓笔记","Author":"南派三叔"},{"BookName":"鬼吹灯","Author":"天下霸唱"}]Name:明朝那些事Author:当年明月Name:盗墓笔记Author:南派三叔Name:鬼吹灯Author:天下霸唱混合使用:{"List":"[{\"BookName\":\"明朝那些事\",\"Author\":\"当年明月\"},{\"BookName\":\"盗墓笔记\",\"Author\":\"南派三叔\"},{\"BookName\":\"鬼吹灯\",\"Author\":\"天下霸唱\"}]","Info":"上地"}List:[{"BookName":"明朝那些事","Author":"当年明月"},{"BookName":"盗墓笔记","Author":"南派三叔"},{"BookName":"鬼吹灯","Author":"天下霸唱"}]--Info:上地Name:明朝那些事Author:当年明月Name:盗墓笔记Author:南派三叔Name:鬼吹灯Author:天下霸唱

  周末在博客园看文章的都是上进的孩子,大家周末愉快~

Android数据解析-JSON解析