在这里我想补充的一点是:在实际的开发当中,我们一般会将我们的json文件放在assets文件目录下。我们可以在项目中,点击new-->Folder---->AssetsFolder.然后再在AssetsFolder中创建json文件。比如说是test.json.那么如果json文件是放在assets下的,那么我们应该怎么进行操作呢?我们只要看一下下面的例子就清楚了.
InputStream is = null;
JsonReader reader=null;
try { is = getAssets().open("test.json"); reader = new JsonReader(new InputStreamReader(is)); reader.beginObject(); while (reader.hasNext()) { /*在这里需要注意的是,这里的话,应该是reader.nextName(),而不是reader.nextString()*/ String keyName = reader.nextName(); if ("name".equals(keyName)) { String name = reader.nextString(); Log.i("JSON------->", name); } else if ("age".equals(keyName)) { int age = reader.nextInt(); Log.i("JSON------->", age + ""); } else if ("weight".equals(keyName)) { int weight = reader.nextInt(); Log.i("JSON------->", weight + ""); } } reader.endObject();} catch (IOException e) { e.printStackTrace();}
test.json文件的内容如下:
{"name":"sam",
"age":18,
"weight":60}
文/追风917(简书作者)
原文链接:http://www.jianshu.com/p/c091918429e9
InputStream is = null; JsonReader reader=null;try { is = getAssets().open("test.json"); reader = new JsonReader(new InputStreamReader(is)); reader.beginObject(); while (reader.hasNext()) { /*在这里需要注意的是,这里的话,应该是reader.nextName(),而不是reader.nextString()*/ String keyName = reader.nextName(); if ("name".equals(keyName)) { String name = reader.nextString(); Log.i("JSON------->", name); } else if ("age".equals(keyName)) { int age = reader.nextInt(); Log.i("JSON------->", age + ""); } else if ("weight".equals(keyName)) { int weight = reader.nextInt(); Log.i("JSON------->", weight + ""); } } reader.endObject();} catch (IOException e) { e.printStackTrace();}
test.json文件的内容如下:
{"name":"sam", "age":18, "weight":60}
tags: [Android, JSON]
JSON是什么
- JSON的全程是JavaScript Object Notation,也就是JavaScript 对象表示法
- JSON是存储和交换文本信息的语法,类似XML,但是比XML更小、更快,更易解析
- JSON是轻量级的文本数据交换格式,独立于语言,具有自我描述性,更易理解
JSON 使用 Javascript语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。 目前非常多的编程语言(PHP,JSP,.NET)都支持JSON。
<!--more-->
JSON 语法
JSON语法 是 JavaScript 对象表示法语法的子集
- 数据在 名称/值对 中
- 数据由逗号分隔
- 花括号{}保存对象
- 方括号[]保存数组
JSON 名称/值对
名称/值对 包括字段名称(在双引号中),后面写一个冒号,然后是值,比如:
"name" : "zhangsan"
"age" : 15
在Android中等价于下面的语句:
String name = "zhangsan";
int age = 15;
JSON 值
JSON 值可以是:
- 数字(整数或浮点数)
- 字符串(在双引号中)
- 逻辑值(true 或 false)
- 数组(在方括号[]中)
- 对象(在花括号{}中)
- null
JSON 对象
JSON 对象在花括号{}中书写:
对象可以包含多个名称/值对,比如:
{"name":"zhangsan" , "age":25}
这一点也容易理解,与这条语句等价:
name = "zhangsan";
age = 25;
JSON 数组
JSON 数组在方括号[]中书写:
数组可包含多个对象,比如:
{"student":[{"name":"zhangsan" , "age":12},{"name":"lisi" , "age":13},{"name":"wangwu" , "age":14}]}
在上面的例子中,对象 "student" 是包含三个对象的数组。每个对象代表一条关于某人(有姓名年龄)的记录。
Android解析JSON
安卓开发文档中为我们提供了org.json包,可以用JSONObject和JSONArray来解析json。android3.0又提供了android.util包JsonReader和JsonWriter来进行json的解析和生成。为了后面方便解析,我们这里先三个典型的json数据(一般从网络返回的都是字节流数据):
InputStream json1 = {"name":"sam","age":18,"weight":60} //一个json对象
InputStream json2 = [12,13,15] //json2 一个json数组
InputStream json3 = [{"name":"sam","age":18},{"name":"leo","age":19},{"name":"sky", "age":20}] //含对象的数组
1. 使用android.util包下的JsonReader进行解析
JsonReader的使用和xml解析中的pull有一点类似的,好了,我们开始吧:
- json1的解析
JsonReader reader = new JsonReader(new InputStreamReader(json1));
try{
reader.beginObject();
while(reader.hasNext()){
String keyName = reader.nextName();
if("name".equals(keyName)){
String name = reader.nextString();
}else if("age".equals(keyName)){
int age = reader.nextInt();
}else if("weight".equals(keyName)){
int weight = reader.nextInt();
}
}
reader.endObject();
}
finally{
reader.close();
}
- json2的解析
JsonReader reader = new JsonReader(new InputStreamReader(json2));
try {
List<Integer> ages = new ArrayList<Integer>();
reader.beginArray();
while (reader.hasNext()) {
ages.add(reader.nextInt());
}
reader.endArray();
}
finally {
reader.close();
}
- json3的解析
JsonReader reader = new JsonReader(new InputStreamReader(json3));
try {
List<Object> ages = new ArrayList<Object>();
reader.beginArray();
while (reader.hasNext()) {
reader.biginObject();
while(reader.hasNext()){
String keyName = reader.nextName();
if("name".equals(keyName)){
String name = reader.nextString();
}else if("age".equals(keyName)){
int age = reader.nextInt();
}else if("weight".equals(keyName)){
int weight = reader.nextInt();
}
}
reader.endObject();
}
reader.endArray();
}
finally {
reader.close();
}
这里有个问题就是解析到while(reader.hasNext())进行不下去,是什么情况,求解??
2. 使用org.json包中的JSONObject(解析对象)和JSONArray(解析数组)进行解析
我们知道json中就两种结构array和object,因此就有这个两个类对上面的数据进行解析。
- json1的解析
JSONObject jsonObj = new JSONObject(json1);
String name = jsonObj.optString("name");
int age = jsonObj.optInt("age");
int weight = jsonObj.optInt("weight");
该类里的其他方法,用于解析不同数据类型数据
Object opt(String name)
boolean optBoolean(String name)
double optDouble(String name)
JSONArray optJSONArray(String name)
JSONObject optJSONObject(String name)
推荐用这些方法,这些方法在解析时,如果对应字段不存在会返回空值或者0,不会报错。
当然还可以使用以下方法
Object get(String name)
boolean getBoolean(String name)
int getInt(String name)
使用这些方法时,代码不会去判断是否存在该字段,需要你自己去判断,否则的话会报错。自己判断的话使用has(String name)来判断。
- json2的解析
JSONArray jsonArray = new JSONArray(json2);
for (int i= 0; i < jsonArray.length(); i++) {
int age = jsonArray.optInt(i);
}
- json3的解析
JSONArray jsonArray = new JSONArray(json3);
for (int i= 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
String name = jsonObject.optString("name");
int age = jsonObject.optInt("age");
}
从上面可以看到数组的解析方法和对象差不多,只是将键值替换为在数组中的下标。另外也是有optXXX(int index)和getXXX(int index)方法的,optXXX方法也是安全的,即对应index无值的时候,不会报错,返回空,推荐使用。
3. 使用谷歌的GSON包进行解析
as下如何导入jar包看这里:
- json1的解析
我们这里的实体类是Student.class
Gson gson = new Gson();
Student student = gson.fromJson(json1, Student.class);
- json2的解析
我们可以解析成int数组,也可以解析成Integer的List。
解析成数组:
Gson gson = new Gson();
int[] ages = gson.fromJson(json2, int[].class);
解析成List:
Gson gson = new Gson();
List<Integer> ages = gson.fromJson(json2, new TypeToken<List<Integer>>(){}.getType);
- json2的解析
同样可以解析成List或者数组,我们就直接解析成List.
Gson gson = new Gson();
List<Student> students = gson.fromJson(json3, new TypeToke<List<Student>>(){}.getType);
从上面的代码看到,使用Gson解析的话就非常简单了。fromJson方法的第一个参数可以为字符串或者InputStreamReader流
结语
刚才介绍了json以及在安卓下的解析,这个是我们经常用到的,在此记录下。在json的解析方法中,我们可以看到使用Gson库的解析是十分简单,这可以为我们的开发节约很多的时间,然后3.0后增加的JsonReader的解析更加流畅,和Pull解析xml的方式有些类似,传统的JsonArray和JsonObject解析的也是可以的,注意使用方法中的optXXX更优哦。
对于json的高级解解析以及生成json以后用到在深入,谢谢!!!
悦分享,越快乐。^_^