本篇博客部分内容转自:https://blog.csdn.net/u013005791/article/details/72904217#22-jsonarray
本篇博客介绍三种json生成方式:组拼字符串、JSONObject+JSONArray、GSON
1 json的格式介绍
1.1 json对象
{"name": "jadyli", "gender": "male"}
对象表示为键值对,数据由逗号分隔,花括号保存对象,使用双引号包裹键名,使用冒号分隔键和值。如果值是String类型且含有双引号或冒号,需要使用”\”转义。
1.2 json数组
[
{"name": "jadyli", "gender": "male"},
{"name": "sanqi", "gender": "male"}
]
方括号保存数组。
2.组拼字符串生成json
String json = "{\"name\":\"《世界的支点》\",\"author\":\"张三\",\"price\":\"50\"}"; File file = new File(Environment.getExternalStorageDirectory() , "book.json"); FileOutputStream fos = new FileOutputStream(file); fos.write(json.getBytes());
相比于结构性非常强的xml文件来说,json数据没有那么多的标签,所以我们组拼起来会更加轻松,组拼完成后可以通过输出流生成json文件。
3.JSONObject+JSONArray生成json
3.1.JSONObject生成单个json对象
JSONObject jsonObject = new JSONObject(); jsonObject.put("name","《世界的支点》");//按键值对的方式存储json对象 jsonObject.put("author","张三"); jsonObject.put("price","55"); String json = jsonObject.toString();//转为字符串再写入输出流 File file = new File(Environment.getExternalStorageDirectory() , "book.json"); FileOutputStream fos = new FileOutputStream(file); fos.write(json.getBytes());
利用谷歌给我们提供的键值对的方法来组拼json对象。
3.2.JSONObject+JSONArray生成json数组
String[] name = {"《世界的支点》","《爱》","《金与紫金》"}; String[] author = {"张三","李四","赵云"}; String[] price = {"50.00","24.00","32.00"}; JSONArray jsonArray = new JSONArray(); for (int i = 0; i < name.length; i ++){ JSONObject jsonObject = new JSONObject(); jsonObject.put("name",name[i]);//对象按键值对存储 jsonObject.put("author",author[i]); jsonObject.put("price",price[i]); jsonArray.put(i,jsonObject);//把对象加入数组 } String json = jsonArray.toString();//数组转字符串以便写入输出流 File file = new File(Environment.getExternalStorageDirectory() , "book.json"); FileOutputStream fos = new FileOutputStream(file); fos.write(json.getBytes());
把3.1生成的多个json对象格式转变成json数组格式,最后再把这个json数组转成字符串写入到输出流之中。
4.Gson方式生成json
要用gson,首先要添加依赖:
compile 'com.google.code.gson:gson:2.7'
其次,需要创建一个JavaBean对象,我以Book类为例。
public class Book { private String name; private String author; private String price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
4.1Gson生成单个json对象
try{ Gson gson = new Gson(); //设置Book的属性 Book book = new Book(); book.setName("《世界的支点》"); book.setAuthor("张三"); book.setPrice("35.00"); String json = gson.toJson(book);//封装book对象并转为字符串以便写入输出流 File file = new File(Environment.getExternalStorageDirectory(), "book.json"); FileOutputStream fos = new FileOutputStream(file); fos.write(json.getBytes()); }catch(Exception e){ }
4.2Gson生成json数组对象
try { String[] name = {"《世界的支点》","《爱》","《金与紫金》"}; String[] author = {"张三","李四","赵云"}; String[] price = {"50.00","24.00","35.00"}; ArrayList<Book> bookList = new ArrayList<>(); Gson gson = new Gson(); //设置Book属性并加入集合 for (int i=0; i<name.length; i++){ Book book = new Book(); book.setName(name[i]); book.setAuthor(author[i]); book.setPrice(price[i]); bookList.add(book); } String json = gson.toJson(bookList);//封装集合并转为字符串以便写入输出流 File file = new File(Environment.getExternalStorageDirectory(), "book.json"); FileOutputStream fos = new FileOutputStream(file); fos.write(json.getBytes()); Log.d(TAG, "onCreate: 创建成功"); } catch (Exception e) { e.printStackTrace(); }
5.Gson和JSONObject的区别
{"name": "《世界的支点》", "author": "张三", "price":"35.00"}
JSONObject就是把json这串字符串作为对象封装起来了,一组这种字符串(包括大括号),就是一个JSONObject对象。
Gson是把这串字符串代表的对象封装起来,比如这串字符串反映的就是Book对象。