轉自:http://www.jb51.net/article/70875.htmjava
首先介紹下JSON的定義,JSON是JavaScript Object Notation的縮寫。android
一種輕量級的數據交換格式,具備良好的可讀和便於快速編寫的特性。業內主流技術爲其提供了完整的解決方案(有點相似於正則表達式,得到了當今大部分語言的支持),從而能夠在不一樣平臺間進行數據交換。JSON採用兼容性很高的文本格式,同時也具有相似於C語言體系的行爲。
正則表達式
JSON的結構:
(1) Name/Value Pairs(無序的):相似所熟知的Keyed list、 Hash table、Disctionary和Associative array。在Android平臺中同時存在另一個類 "Bundle",某種程度上具備類似的行爲。json
(2) Array(有序的):一組有序的數據列表。app
一: Json的特性和在數據交互中的地位就不用說了,直接看案例。eclipse
首先在android studio中建立assets文件目錄,用於存放Json數據文件,android studio 1.3 默認項目文件目錄下是沒有assets文件夾的,ide
因此須要咱們進行建立,建立方法以下:ui
建立好assets文件目錄之後,在其目錄下建立一個Text.json文件。this
二:如何得到assets文件目錄下的Json數據:spa
在eclipse下是:InputStreamReader(getAssets().open("Text.json"),"UTF-8");得到該文件數據,並以InputStream返回數據。
而在android studio則是經過:JsonLearn.this.getClass().getClassLoader().getResourceAsStream("assets/" + "Text.json");返回相應InputStream.
三:案例展現:
1:案例項目app界面以下,經過按鈕分別實現Json數據的讀取和建立,並展現在TextView中。
2:代碼以下:
package activity.cyq.datalrearn; import android.support.v.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class JsonLearn extends AppCompatActivity { private TextView writeText, readText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_json_learn); readText = (TextView) findViewById(R.id.readJsonText); writeText = (TextView) findViewById(R.id.writeJsonText); /*讀取Json數據*/ findViewById(R.id.readJsioBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /*獲取到assets文件下的TExt.json文件的數據,並以輸出流形式返回。*/ InputStream is = JsonLearn.this.getClass().getClassLoader().getResourceAsStream("assets/" + "Text.json"); InputStreamReader streamReader = new InputStreamReader(is); BufferedReader reader = new BufferedReader(streamReader); String line; StringBuilder stringBuilder = new StringBuilder(); try { while ((line = reader.readLine()) != null) { // stringBuilder.append(line); stringBuilder.append(line); } reader.close(); reader.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } try { JSONObject person = new JSONObject(stringBuilder.toString()); JSONArray infArray = person.getJSONArray("inf"); for (int i = ; i < infArray.length(); i++) { JSONObject inf_Array = infArray.getJSONObject(i); readText.append("name:" + inf_Array.getString("name") + "\n"); readText.append("IdCard:" + inf_Array.getString("IdCard")); readText.append("age:" + inf_Array.getInt("age")); readText.append("married:" + inf_Array.getBoolean("married")); } } catch (JSONException e) { e.printStackTrace(); } } }); /*建立Json數據並顯示*/ findViewById(R.id.writeJsioBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { JSONObject inf = new JSONObject(); inf.put("number", ); JSONArray array = new JSONArray(); JSONObject arr_ = new JSONObject(); arr_.put("name", "張三"); arr_.put("age", ); arr_.put("IdCard", "XC"); arr_.put("married", true); JSONObject arr_ = new JSONObject(); arr_.put("name", "李四"); arr_.put("age", ); arr_.put("IdCard", "@DC"); arr_.put("married", true); array.put(, arr_); array.put(, arr_); inf.put("inf", array); writeText.setText(inf.toString()); } catch (JSONException e) { e.printStackTrace(); } } }); } }