標籤:
一.JSON的簡介:
JSON建構於兩種結構:
(1)“名稱/值”對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),雜湊表(hash table),有鍵列表(keyed list),或者關聯陣列 (associative array)。
(2)值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)。
這些都是常見的資料結構。事實上大部分現代電腦語言都以某種形式支援它們。這使得一種資料格式在同樣基於這些結構的程式設計語言之間交換成為可能。
JSON具有以下這些形式:
對象是一個無序的“‘名稱/值’對”集合。一個對象以“{”(左括弧)開始,“}”(右括弧)結束。每個“名稱”後跟一個“:”(冒號);“‘名稱/值’ 對”之間使用“,”(逗號)分隔。
數組是值(value)的有序集合。一個數組以“[”(左中括弧)開始,“]”(右中括弧)結束。值之間使用“,”(逗號)分隔。
值(value)可以是雙引號括起來的字串(string)、數值(number)、true、false、 null、對象(object)或者數組(array)。這些結構可以嵌套。
字串(string)是由雙引號包圍的任意數量Unicode字元的集合,使用反斜線轉義。一個字元(character)即一個單獨的字串(character string)。
字串(string)與C或者Java的字串非常相似。
數值(number)也與C或者Java的數值非常相似。除去未曾使用的八進位與十六進位格式。除去一些編碼細節。
二.ANDROID中JSON的API簡介:
1.JSONObject,JSONArry,JSONStringer類構建json檔案
2.getType可以將要擷取的鍵的值轉換為指定的類型,如果無法轉換或沒有值得時候拋出異常JSONRException
optType也可以將要擷取的鍵的值轉換為指定的類型,如果無法轉換或沒有值得時候返回使用者提供或預設提供的值
三.Android中對於JSON的使用:
介面:
MainActivity的主要代碼:
package com.example.creatjson;import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private JSONObject mContactJson;private File file = null;private FileOutputStream fileOutputStream = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}private void init() {Button btCreatJSON = (Button) findViewById(R.id.bt_write_json);btCreatJSON.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, ShowJSON.class);intent.putExtra("JSON", creatJSON());startActivity(intent);}});Button btReadJSON = (Button) findViewById(R.id.bt_read_json);btReadJSON.setOnClickListener(new OnClickListener() {String line;@Overridepublic void onClick(View v) {try {InputStreamReader inputStreamReader = new InputStreamReader(getAssets().open("Json.json"), "UTF-8");BufferedReader bufferedReader = new BufferedReader(inputStreamReader);StringBuilder stringBuilder = new StringBuilder();while ((line = bufferedReader.readLine()) != null) {stringBuilder.append(line);}inputStreamReader.close();bufferedReader.close();JSONObject mContactJson = new JSONObject(stringBuilder.toString());line = null;line += "stranger:" + mContactJson.get("stranger") + "\n";JSONArray jsonArray = mContactJson.getJSONArray("friend");for (int i = 0; i < jsonArray.length(); i++) {JSONObject jsonObject = jsonArray.getJSONObject(i);line += "friendname:"+ jsonObject.getString("friendname")+ " number:" + jsonObject.getString("number")+ "\n";}} catch (Exception e) {e.printStackTrace();}Intent intent = new Intent(MainActivity.this, ShowJSON.class);intent.putExtra("JSON", line);startActivity(intent);}});}private String creatJSON() {try {mContactJson = new JSONObject();JSONArray mFriend = new JSONArray();JSONObject friend1 = new JSONObject();friend1.put("friendname", "zhangsan");friend1.put("number", "131456789");JSONObject friend2 = new JSONObject();friend2.put("friendname", "lisi");friend2.put("number", "131456789");JSONObject friend3 = new JSONObject();friend3.put("friendname", "liuwu");friend3.put("number", "131456789");mFriend.put(friend1);mFriend.put(friend2);mFriend.put(friend3);mContactJson.put("friend", mFriend);mContactJson.put("stranger", "134651313");} catch (JSONException e) {e.printStackTrace();}return mContactJson.toString();}}
Android_JSON資料解析Demo:http://download.csdn.net/detail/two_water/9388361
Android_JSON資料解析