Json初识
2016-10-19 15:55
169 查看
Json
1.注意事项
1. 解析的时候 需传入 Json 格式的字符串 JSONArray jsonArray = new JSONArray(string), 然后调用get 方法取值; 2. 生成 Json 的时候 JSONArray jsonArray = new JSONArray() 不需要传入数据,值需要调用 put 方法 放入即可。
2.定义
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。3.特点
平台独立 易于阅读、编写 读取速度快,效率高
4.JSON 语法规则
JSON 语法是 JavaScript 对象表示语法的子集。 数据在键值对中 数据由逗号分隔 花括号保存对象 方括号保存数组
5. JSON 名称/值对
JSON 数据的书写格式是:名称/值对。 名称/值对组合中的名称写在前面(在双引号中),值对写在后面(同样在双引号中),中间用冒号隔开: “firstName”:“John” 等价于这条 JavaScript 语句: firstName=“John”
6. JSON 值
数字(整数或浮点数) 字符串(在双引号中) 逻辑值(true 或 false) 数组(在方括号中) 对象(在花括号中) null
7. JSON 结构
两种结构: 对象和数组 对象:{名称:值,名称:值} 数组: ["java","javascript","vb",...] eg:
* 对象
{"firstName":"Brett"} {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}
数组
{ "people":[ {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}, {"firstName":"Jason","lastName":"Hunter","email":"bbbb"}, {"firstName":"Elliotte","lastName":"Harold","email":"cccc"} ] }
8. 解析
JSONObject : //把json字符串转为json对象JSONObject jobj = new JSONObject(str); jobj.optString(String name)… jobj. optJSONArray() jobj. optJSONObject()
JSONArray: //把json串转为json数组
JSONArray jarr = new JSONObject(str); jarr.getString(int index)… Jarr.getInt(int index) jarr.length()—长度
9. 示例代码:
一. 解析 与 生成 Json 9.1 获取解析文件 放入 assets中 最简单对象型{"name":"小明","age":12} 数组型[{"name":"杨过1","age":12},{"name":"杨过12","age":13},{"name":"杨过122","age":22}] 混合型[{"info": {"address": "山西","age": "12"},"user": "杨过"}] 9.2 布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="read_object" android:layout_weight="1" android:text="简单json解析" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="read_array" android:layout_weight="1" android:text="json解析数组" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="read_array_and_object" android:text="json解析混合 数组和对象" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="createJsonObject" android:layout_weight="1" android:text="生成json对象" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="createJsonArray" android:layout_weight="1" android:text="生成json数组" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="create_array_and_object" android:text="生成json混合 数组和对象" /> </LinearLayout> <TextView android:id="@+id/main_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="数据测试:" android:textSize="20sp" /> </LinearLayout> 9.3 主代码 public class MainActivity extends Activity { private TextView main_tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1. 初始化控件 initViews(); } private void initViews() { // 初始胡控件 main_tv = (TextView) findViewById(R.id.main_tv); } // 设置点击方法 public void read_object(View v) { main_tv.setText(""); try { // 1. 获取json 字符串 String json = getString(getAssets().open("json1.json")); // 2. 获取 json 对象 JSONObject jsonObject = new JSONObject(json); // 3. 从 json 对象中取出数据 String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); main_tv.setText("name:" + name + "age:" + age); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void read_array(View v) { main_tv.setText(""); // 1. 获取字符串 json try { // 1. 获取 json 字符串 String json = getString(getAssets().open("json2.json")); // 2. 获取 格式 数组格式 JSONArray jsonArray = new JSONArray(json); // 3. 遍历数组 for (int i = 0; i < jsonArray.length(); i++) { // 获得数组对象 JSONObject jsonObject = jsonArray.getJSONObject(i); // 从对象中获取 数据 String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); main_tv.append("name:" + name + "age:" + age + "\n"); } } catch (Exception e) { e.printStackTrace(); } } public void read_array_and_object(View v) { main_tv.setText(""); try { // 1. 获取json字符串 InputStream open = getAssets().open("json3.json"); String string = getString(open); // 2. 获得json 数组对象 JSONArray jsonArray = new JSONArray(string); // 3. 遍历数组 for (int i = 0; i < jsonArray.length(); i++) { // 4. 获取数组中的对象 JSONObject jsonObject = jsonArray.getJSONObject(i); // 5. 从该对象中获取 相关信息 String user = jsonObject.getString("user"); // 6. 从该对象中获取对象信息 JSONObject jsonObject2 = jsonObject.getJSONObject("info"); // 7. 从最新的对象中获取 相关信息 String address = jsonObject2.getString("address"); String age = jsonObject d0fb 2.getString("age"); main_tv.append("user:" + user + "address:" + address + "age:" + age); } } catch (Exception e) { e.printStackTrace(); } } public void createJsonObject(View v) { main_tv.setText(""); // 创建 json 对象 JSONObject jsonObject = new JSONObject(); // 放入数据 try { jsonObject.put("name", "秦始皇"); jsonObject.put("sex", "男"); // 数据展示 String string = jsonObject.toString(); main_tv.append(string); } catch (JSONException e) { e.printStackTrace(); } } public void createJsonArray(View v) { main_tv.setText(""); // 创建 json 数组对象 JSONArray jsonArray = new JSONArray(); // 获得 json 对象 for (int i = 0; i < 4; i++) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("春天", "春困" + i); jsonObject.put("夏天", "夏打盹" + i); jsonObject.put("秋天", "秋乏" + i); jsonObject.put("冬天", "冬眠" + i); } catch (JSONException e) { e.printStackTrace(); } jsonArray.put(jsonObject); } String string = jsonArray.toString(); main_tv.setText(string); } // [{"user":"汉武帝","info":{"time":"西汉","fell":"影响大"}}] public void create_array_and_object(View v) { main_tv.setText(""); // 获取 数组 JSONArray jsonArray = new JSONArray(); try { // 获取对象 JSONObject jsonObject = new JSONObject(); jsonObject.put("user", "汉武帝"); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("time", "西汉"); jsonObject2.put("fell", "影响大"); jsonObject.putOpt("info", jsonObject2); jsonArray.put(jsonObject); } catch (JSONException e) { e.printStackTrace(); } String string = jsonArray.toString(); main_tv.append(string); } public String getString(InputStream in) throws IOException { // 字节数组输出流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = 0; while ((len = in.read(b)) != -1) { // 写入输出流 baos.write(b, 0, len); } return baos.toString(); } }
相关文章推荐
- JSON解析---初识
- 初识Json
- JSON.stringify初识
- 【原】轻量级的数据交换格式——初识Json(下)
- 初识JSON
- 初识JSON
- What is JSON:初识JSON
- 初识--Ajax & Json
- 初识JSON
- 国人:JSON-RPC之初识
- Java学习-027-JSON 之一 -- 初识
- 【原】轻量级的数据交换格式——初识Json(上)
- Json初识总结
- Java中json的使用之初识JSON(一)
- Go实战--也许最快的Go语言Web框架kataras/iris初识(basic认证、Markdown、YAML、Json)
- 初识JSON
- 轻量级的数据交换格式——初识Json(上)
- [一]初识Json
- JSON初识
- 【转】What is JSON:初识JSON