JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。 易于人阅读和编写。 同时也易于机器解析和生成。
1. 作用:存储数据(对象),传输数据(对象)。
2. 解析:就是把json文档中的数据,转化为JSON格式字符串,再把字符串转化为对象
第一步:
/**
* 读取文档数据
* @return
*/
public static String readFileJson(String path){
File file =new File(path);
3. BufferedReader br =null;
4. StringBuilder sb =new StringBuilder();
5. try {
6. br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
7. String line =null;
8. while((line=br.readLine())!=null){
9. sb.append(line);
}
10. } catch (FileNotFoundException e) {
11. e.printStackTrace();
12. } catch (IOException e) {
13. e.printStackTrace();
14. }finally{
15. try {
16. if(br!=null){
17. br.close();
18. }
19. } catch (IOException e) {
20. e.printStackTrace();
21. }
22. }
23. return sb.toString();
24.
25. }
第二步:
26. /**
27. * 读文件
28. * @throws JSONException
29. */
30. public void readFile() throws JSONException {
31. String jsonStr = readFileJson("Books.json");
32. List<Book> books=parseJson(jsonStr);
33. for(Book book : books){
34. System.out.println(book);
35. }
36. }
第三部:
37. /**
38. * 解析JSON文档 由外到里
39. * @param json
40. * @return
41. * @throws JSONException
42. */
43.
44. public static List<Book> parseJson(String json) throws JSONException {
45. List<Book> books = new ArrayList<>();
46. JSONObject rootJson = new JSONObject(json);// 字符串转化为JSON文档对象
47. JSONArray bookArray = rootJson.getJSONArray("books");
48. for (int i = 0; i < bookArray.length(); i++) {
49. Book book = new Book();
50. // JSONObject bookJsonParent = bookArray.getJSONObject(i);
51. // JSONObject bookJson =bookJsonParent.getJSONObject("book");
52. JSONObject bookJson = bookArray.getJSONObject(i);
53. int id = bookJson.getInt("id");
54. book.setId(id);
55. String name = bookJson.getString("name");
56. book.setName(name);
57. double price = bookJson.getDouble("price");
58. book.setPrice(price);
59.
60. books.add(book);
61. }
62. return books;
63. }
64. 生成:把对象中的内容转化为JSON格式的字符串,然后写入json文档。
第一步:
/**
* 生成列表 由里到外
*
* @throws JSONException
* @throws IOException
*/
public static void creatBooksJson() throws JSONException, IOException {
JSONObject bookjson1 = new JSONObject();
bookjson1.put("id", 1001);
bookjson1.put("name", "Java编程思想");
bookjson1.put("price", 98);
JSONObject bookjson2 = new JSONObject();
bookjson2.put("id", 1002);
bookjson2.put("name", "web前端技术");
bookjson2.put("price", 78);
JSONObject bookjson3 = new JSONObject();
bookjson3.put("id", 1003);
bookjson3.put("name", "Mysql");
bookjson3.put("price", 88);
JSONArray booksArray = new JSONArray();
booksArray.put(bookjson1);
booksArray.put(bookjson2);
booksArray.put(bookjson3);
System.out.println(booksArray.toString());
JSONObject booksObject = new JSONObject();
booksObject.put("books", booksArray);
System.out.println(booksObject);
setFile(booksObject.toString());
}
第二步:
/**
* 写入文件
*
* @param str
* @throws IOException
*/
public static void setFile(String str) throws IOException {
FileWriter file = new FileWriter("Books.json");
file.write(str);
file.close();
1. 作用:存储数据(对象),传输数据(对象)。
2. 解析:就是把json文档中的数据,转化为JSON格式字符串,再把字符串转化为对象
第一步:
/**
* 读取文档数据
* @return
*/
public static String readFileJson(String path){
File file =new File(path);
3. BufferedReader br =null;
4. StringBuilder sb =new StringBuilder();
5. try {
6. br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
7. String line =null;
8. while((line=br.readLine())!=null){
9. sb.append(line);
}
10. } catch (FileNotFoundException e) {
11. e.printStackTrace();
12. } catch (IOException e) {
13. e.printStackTrace();
14. }finally{
15. try {
16. if(br!=null){
17. br.close();
18. }
19. } catch (IOException e) {
20. e.printStackTrace();
21. }
22. }
23. return sb.toString();
24.
25. }
第二步:
26. /**
27. * 读文件
28. * @throws JSONException
29. */
30. public void readFile() throws JSONException {
31. String jsonStr = readFileJson("Books.json");
32. List<Book> books=parseJson(jsonStr);
33. for(Book book : books){
34. System.out.println(book);
35. }
36. }
第三部:
37. /**
38. * 解析JSON文档 由外到里
39. * @param json
40. * @return
41. * @throws JSONException
42. */
43.
44. public static List<Book> parseJson(String json) throws JSONException {
45. List<Book> books = new ArrayList<>();
46. JSONObject rootJson = new JSONObject(json);// 字符串转化为JSON文档对象
47. JSONArray bookArray = rootJson.getJSONArray("books");
48. for (int i = 0; i < bookArray.length(); i++) {
49. Book book = new Book();
50. // JSONObject bookJsonParent = bookArray.getJSONObject(i);
51. // JSONObject bookJson =bookJsonParent.getJSONObject("book");
52. JSONObject bookJson = bookArray.getJSONObject(i);
53. int id = bookJson.getInt("id");
54. book.setId(id);
55. String name = bookJson.getString("name");
56. book.setName(name);
57. double price = bookJson.getDouble("price");
58. book.setPrice(price);
59.
60. books.add(book);
61. }
62. return books;
63. }
64. 生成:把对象中的内容转化为JSON格式的字符串,然后写入json文档。
第一步:
/**
* 生成列表 由里到外
*
* @throws JSONException
* @throws IOException
*/
public static void creatBooksJson() throws JSONException, IOException {
JSONObject bookjson1 = new JSONObject();
bookjson1.put("id", 1001);
bookjson1.put("name", "Java编程思想");
bookjson1.put("price", 98);
JSONObject bookjson2 = new JSONObject();
bookjson2.put("id", 1002);
bookjson2.put("name", "web前端技术");
bookjson2.put("price", 78);
JSONObject bookjson3 = new JSONObject();
bookjson3.put("id", 1003);
bookjson3.put("name", "Mysql");
bookjson3.put("price", 88);
JSONArray booksArray = new JSONArray();
booksArray.put(bookjson1);
booksArray.put(bookjson2);
booksArray.put(bookjson3);
System.out.println(booksArray.toString());
JSONObject booksObject = new JSONObject();
booksObject.put("books", booksArray);
System.out.println(booksObject);
setFile(booksObject.toString());
}
第二步:
/**
* 写入文件
*
* @param str
* @throws IOException
*/
public static void setFile(String str) throws IOException {
FileWriter file = new FileWriter("Books.json");
file.write(str);
file.close();
}
谷歌Json框架
Gson gson = new Gson();
String bookJson =gson.toJson(book);//将java对象转换成json字符串
System.out.println(bookJson);
Gson gson = new Gson();
CityResult cityResult = gson.fromJson(json,CtiyResult.class);
return cityResult;