JSON即JavaScript Object Natation, 它是一种轻量级的数据交换格式, 与XML一样, 是广泛被采用的客户端和服务端交互的解决方案.接下来由脚本之家小编给大家介绍Android中gson、jsonobject解析JSON的方法,感兴趣的朋友一起学习吧
正文
Android中gson、jsonobject解析JSON的方法详解
json的定义:
一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。json采用兼容性很高的文本格式,同时也具备类似于c语言体系的行为。
json对象:
json中对象(object)以"{"开始, 以"}"结束. 对象中的每一个item都是一个key-value对, 表现为"key:value"的形式, key-value对之间使用逗号分隔. 如:{"name":"coolxing", "age"=24, "male":true, "address":{"street":"huilongguan", "city":"beijing", "country":"china"}}. json对象的key只能是string类型的, 而value可以是string, number, false, true, null, object对象甚至是array数组, 也就是说可以存在嵌套的情况.
json数组:
json数组(array)以"["开始, 以"]"结束, 数组中的每一个元素可以是string, number, false, true, null, object对象甚至是array数组, 数组间的元素使用逗号分隔. 如["coolxing", 24, {"street":"huilongguan", "city":"beijing", "country":"china"}].
1.前言
json数据是android网络开发中常见的数据格式。解析json数据有多种方法。
1.1 使用官方自带jsonobject
1.2 使用第三方开源库,包括但不限于 gson 、 fastjson 、 jackson ,本文主要介绍由google提供的gson库的使用方法。
2.jsonobject的使用方法
2.1 示例代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//org.json.jsonarray;//org.json.jsonobject;private void parsejsonwithjsonobject(string jsondata){try {//将json字符串jsondata装入json数组,即jsonarray//jsondata可以是从文件中读取,也可以从服务器端获得jsonarray jsonarray = new jsonarray(jsondata);for (int i = 0; i< jsonarray.length(); i++) {//循环遍历,依次取出jsonobject对象//用getint和getstring方法取出对应键值jsonobject jsonobject = jsonarray.getjsonobject(i);int stu_no = jsonobject.getint("stu_no");string stu_name = jsonobject.getstring("stu_name");string stu_sex = jsonobject.getstring("stu_sex");log.d("mainactivity","stu_no: " + stu_no);log.d("mainactivity","stu_name: " + stu_name);log.d("mainactivity","stu_sex: " + stu_sex);}} catch (exception e) {e.printstacktrace();}} |
2.2 字符串jsondata如下,图为运行结果
|
1
2
3
|
[{ "stu_no":12345,"stu_name":"john","stu_sex":"male"},{ "stu_no":12346,"stu_name":"tom","stu_sex":"male"},{"stu_no":12347,"stu_name":"lily","stu_sex":"female"}] |

3.gson的使用方法
3.1 下载并安装
•将下载的gson-2.6.1.jar复制到 项目目录->app->libs 文件夹下

3.2 方法简介
•tojson(params1),将传入对象转换为字符串
•fromjson(params1,params2),传入两个参数,将字符串params1转换为params2指定的数据类型。
3.3 示例代码
3.3.1 单个对象的解析
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class student {private int stu_no;private string stu_name;private string stu_sex;student(int stu_no,string stu_name,string stu_sex){this.stu_no = stu_no;this.stu_name = stu_name;this.stu_sex = stu_sex;}}// 序列化,将student对象stu转换为字符串strstudent stu = new student(123,"tom","male");gson gson = new gson();string str = gson.tojson(stu); //反序列化,将字符串转换为student对象jsondata = "{ \"stu_no\":12345,\"stu_name\":\"john\",\"stu_sex\":\"male\" }";gson gson = new gson();student student = gson.fromjson(jsondata,student.class); |
3.3.2 json数组的解析(原生类)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
gson gson = new gson();int[] ints = {1, 2, 3, 4, 5};string[] strings = {"abc", "def", "ghi"};//序列化(serialization)//将整数数组转换为json数组gson.tojson(ints); // ==> [1,2,3,4,5]//将字符串数组转换为json数组gson.tojson(strings); // ==> ["abc", "def", "ghi"]// 反序列化(deserialization)// 将json数组转换为原生类数组// ints2、string2与ints、strings相等int[] ints2 = gson.fromjson("[1,2,3,4,5]", int[].class); string[] strings2 = gson.fromjson("[\"abc\", \"def\", \"ghi\"]",string[].class); |
3.3.3 json数组的解析(自定义类)
|
1
2
3
4
5
6
|
//对于类似于2.2中的jsondata,包含3个student对象//与原生类不同,需要借助typetoken获得期望解析成的数据类型//下列代码运行后,students包含三个student对象gson gson = new gson();list<student> students;students = gson.fromjson(jsondata, new typetoken<list<student>>(){}.gettype()); // ==>[stu0,stu1,stu2] |
3.4 更多方法
•gson的 简便之处 在于其可以将字符串 自动映射 为原生或自定义对象,从而不需要手动编写代码进行解析。
•gson的 更多方法 可以阅读gson在github上的用法介绍,readme.md -> user guide。
以上内容给大家介绍了android中gson、jsonobject解析json的方法详解,希望对大家有所帮助。

发表评论