JSON是什么:

JSON是轻量级的文本数据交换格式

JSON独立于语言和平台

JSON具有自我描述性,更容易理解

JSON语法:

数据在名称/值对中

数据由逗号分割

大括号表示对象

中括号表示数组

JSON使用:

MainActivity.java

  1. package cn.lixyz.jsontest.activity;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.UnsupportedEncodingException;
  9.  
  10. import org.json.JSONArray;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13.  
  14. import android.app.Activity;
  15. import android.os.Bundle;
  16. import android.os.Environment;
  17. import android.util.Log;
  18. import android.view.View;
  19. import cn.lixyz.jsontest.R;
  20.  
  21. public class MainActivity extends Activity {
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27.  
  28. }
  29.  
  30. public void clickButton(View v) {
  31. switch (v.getId()) {
  32. case R.id.bt_readjson:
  33. readJson();
  34. break;
  35. case R.id.bt_writejson:
  36. writeJson();
  37. break;
  38. }
  39. }
  40.  
  41. // 往sdcard中写入文件
  42. private void writeJson() {
  43.  
  44. // 创建一个json对象
  45. JSONObject root = new JSONObject();
  46. try {
  47. // 使用put(kay,value)插入元素
  48. root.put("class", "三年二班");
  49. // 常见若干对象,用以加入数组
  50. JSONObject student1 = new JSONObject();
  51. student1.put("id", 1);
  52. student1.put("name", "张三");
  53. student1.put("age", 10);
  54. JSONObject student2 = new JSONObject();
  55. student2.put("id", 2);
  56. student2.put("name", "李四");
  57. student2.put("age", 11);
  58. JSONObject student3 = new JSONObject();
  59. student3.put("id", 3);
  60. student3.put("name", "王五");
  61. student3.put("age", 13);
  62. JSONObject student4 = new JSONObject();
  63. student4.put("id", 4);
  64. student4.put("name", "赵六");
  65. student4.put("age", 14);
  66. JSONObject student5 = new JSONObject();
  67. student5.put("id", 5);
  68. student5.put("name", "孙七");
  69. student5.put("age", 15);
  70. JSONObject student6 = new JSONObject();
  71. student6.put("id", 6);
  72. student6.put("name", "刘八");
  73. student6.put("age", 16);
  74. // 创建一个json数组
  75. JSONArray array = new JSONArray();
  76. // 将之前创建的json对象添加进来
  77. array.put(student1);
  78. array.put(student2);
  79. array.put(student3);
  80. array.put(student4);
  81. array.put(student5);
  82. array.put(student6);
  83.  
  84. // 将json数组添加
  85. root.put("student", array);
  86. Log.d("TTTT", array.toString());
  87. } catch (JSONException e) {
  88. // TODO Auto-generated catch block
  89. e.printStackTrace();
  90. }
  91.  
  92. String packageName = this.getPackageName();
  93. String jsonFileName = "json.json";
  94. String sdCardPath = Environment.getExternalStorageDirectory().toString();
  95.  
  96. File sdCardPackagePath = new File(sdCardPath + "/" + packageName);
  97. if (!sdCardPackagePath.exists()) {
  98. if (sdCardPackagePath.mkdir()) {
  99. Log.d("TTTT", "创建目录成功");
  100. } else {
  101. Log.d("TTTT", "创建目录不成功");
  102.  
  103. }
  104.  
  105. }
  106.  
  107. File jsonFile = new File(sdCardPackagePath + "/" + jsonFileName);
  108. if (!jsonFile.exists()) {
  109. try {
  110. if (jsonFile.createNewFile()) {
  111. Log.d("TTTT", "创建文件成功");
  112. } else {
  113. Log.d("TTTT", "创建文件失败");
  114.  
  115. }
  116.  
  117. } catch (IOException e) {
  118. // TODO Auto-generated catch block
  119. e.printStackTrace();
  120. }
  121. }
  122.  
  123. try {
  124. // 将json数据写入文件
  125. FileWriter fileWriter = new FileWriter(jsonFile);
  126. fileWriter.write(root.toString());
  127. fileWriter.flush();
  128. } catch (IOException e) {
  129. // TODO Auto-generated catch block
  130. e.printStackTrace();
  131. }
  132. }
  133.  
  134. // 读取assets目录中的json文件
  135. private void readJson() {
  136. try {
  137. // 读取assets目录下的json文件
  138. InputStreamReader isr = new InputStreamReader(getAssets().open("test.json"), "UTF-8");
  139. BufferedReader br = new BufferedReader(isr);
  140. String line;
  141. StringBuilder builder = new StringBuilder();
  142. while ((line = br.readLine()) != null) {
  143. builder.append(line);
  144. }
  145. br.close();
  146. // 获取到json文件中的对象
  147. JSONObject root = new JSONObject(builder.toString());
  148. // 使用getString(key)方式获取value
  149. Log.d("TTTT", "class=" + root.getString("class"));
  150. // 获取json数组
  151. JSONArray array = root.getJSONArray("student");
  152. for (int i = 0; i < array.length(); i++) {
  153. JSONObject student = array.getJSONObject(i);
  154. Log.d("TTTT", "id=" + student.getInt("id") + ",name=" + student.getString("name") + ",age="
  155. + student.getInt("age"));
  156. }
  157. } catch (UnsupportedEncodingException e) {
  158. // TODO Auto-generated catch block
  159. e.printStackTrace();
  160. } catch (IOException e) {
  161. // TODO Auto-generated catch block
  162. e.printStackTrace();
  163. } catch (JSONException e) {
  164. // TODO Auto-generated catch block
  165. e.printStackTrace();
  166. }
  167. }
  168. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="cn.lixyz.jsontest.activity.MainActivity" >
  11.  
  12. <Button
  13. android:id="@+id/bt_writejson"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:onClick="clickButton"
  17. android:text="@string/writejson" />
  18.  
  19. <Button
  20. android:id="@+id/bt_readjson"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:onClick="clickButton"
  24. android:text="@string/readjson" />
  25.  
  26. </LinearLayout>

/assets/test.json

  1. {
  2. "student":[
  3. {"id":1,"name":"张三","age":10},
  4. {"id":2,"name":"李四","age":11},
  5. {"id":3,"name":"王五","age":12},
  6. {"id":4,"name":"赵六","age":13},
  7. {"id":5,"name":"孙七","age":14},
  8. {"id":6,"name":"刘八","age":15},
  9. ],
  10. "class":"三年二班"
  11. }

Android笔记(五十) Android中的JSON数据的更多相关文章

  1. Android(java)学习笔记208:Android中操作JSON数据(Json和Jsonarray)

    1.Json 和 Xml       JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...

  2. Android(java)学习笔记151:Android中操作JSON数据(Json和Jsonarray)

    1.Json 和 Xml       JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...

  3. 截取HTML中的JSON数据并利用GSON进行解析(Android)

    截取HTML中的JSON数据并利用GSON进行解析(Android) 前言 最近在做的一个Android项目,需要自行搭建服务器,队友选择买了阿里云的服务器ESC产品,在数据获取上,我们采用了Andr ...

  4. python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码

    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...

  5. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  6. 关于mysql中存储json数据的读取问题

    在mysql中存储json数据,字段类型用text,java实体中用String接受. 返回前端时(我这里返回前端的是一个map),为了保证读取出的数据排序错乱问题,定义Map时要用LinkedHas ...

  7. JMeter 中对于Json数据的处理方法

    JMeter中对于Json数据的处理方法 http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/ J ...

  8. java读取url中json文件中的json数据

    有时候需要远程从其他接口中获取json数据,如果遇到返回的json数据是一个文件而不直接是数据,那么可以通过以下方法进行读取: /** * 从数据接口获取到数据 * @return * @throws ...

  9. vue中引入json数据,不用本地请求

    1.我的项目结构,需要在Daily.vue中引入daily.js中的json数据 2.把json数据放入一个js文件中,用exports导出,vscode的json格式太严格了,很多数据,调了一个多小 ...

  10. ASP.NET Core中返回 json 数据首字母大小写问题

    ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...

随机推荐

  1. ubuntu16上部署confluence-6.14.5的迁移

    author:headsen chen date:  2019-10-18  15:02:06 notice :created  by  headsen chen himself and not al ...

  2. win10下通过npm成功搭建react开发环境

    1.安装node-v12.13.1-x64(LTS) 2.安装creatre-react-app: npm install -g create-react-app 3.通过create-react-a ...

  3. visual studio(vs)初始化

    cmd 进入到 devenv.exe 所在目录 执行一下命令 devenv.exe /setup /resetuserdata /resetsettings

  4. 【转载】 《Human-level concept learning through probabilistic program induction》阅读笔记

    原文地址: https://blog.csdn.net/ln1996/article/details/78459060 --------------------- 作者:lnn_csdn 来源:CSD ...

  5. 最稳定万能vip视频解析接口 支持HTTPS

    最稳定万能vip视频解析接口 支持HTTPS https://cdn.yangju.vip/k/?url=后面加上播放的地址即可 https://cdn.yangju.vip/k/?url= http ...

  6. sql 表的连接 inner join、full join、left join、right join、natural join

    一.内连接-inner jion : SELECT * FROM table1 INNER JOIN table2 ON table1.field1 compopr table2.field2 INN ...

  7. Jetson TX2

    NVIDIA Jetson TX2作为一个嵌入式平台的深度学习端,具备不错的GPU性能,可以发现TX2的GPU的计算能力是6.2.这意味着TX2对半精度运算有着良好的支持,因此,完全可以在桌面端训练好 ...

  8. KAFA架构及其基本概念

    1.目标 - KAFA价格 在我们上一篇Kafka教程中,我们讨论了Kafka用例和应用程序.今天,在这个Kafka教程中,我们将讨论Kafka Architecture.在这篇Kafka Archi ...

  9. 在Firefox中操作iframe的一个小问题

    在做一个 Web 的打印功能时,需要将被打印的文档写到 iframe 的 document 中. <!doctype html> <html lang="en"& ...

  10. [NOI2008]志愿者招募 (费用流)

    大意: $n$天, 第$i$天要$a_i$个志愿者. $m$种志愿者, 每种无限多, 第$i$种工作时间$[s_i,t_i]$花费$c_i$, 求最少花费. 源点$S$连第一天, 容量$INF$ 第$ ...