欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法

 更新時(shí)間:2015年08月10日 16:08:32   作者:木頭同學(xué)  
android 讀取json數(shù)據(jù),下面小編給大家整理有關(guān)Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法,需要的朋友可以參考下

首先介紹下JSON的定義,JSON是JavaScript Object Notation的縮寫(xiě)。

一種輕量級(jí)的數(shù)據(jù)交換格式,具有良好的可讀和便于快速編寫(xiě)的特性。業(yè)內(nèi)主流技術(shù)為其提供了完整的解決方案(有點(diǎn)類(lèi)似于正則表達(dá)式,獲得了當(dāng)今大部分語(yǔ)言的支持),從而可以在不同平臺(tái)間進(jìn)行數(shù)據(jù)交換。JSON采用兼容性很高的文本格式,同時(shí)也具備類(lèi)似于C語(yǔ)言體系的行為。

JSON的結(jié)構(gòu):
(1) Name/Value Pairs(無(wú)序的):類(lèi)似所熟知的Keyed list、 Hash table、Disctionary和Associative array。在Android平臺(tái)中同時(shí)存在另外一個(gè)類(lèi) "Bundle",某種程度上具有相似的行為。

(2) Array(有序的):一組有序的數(shù)據(jù)列表。

一:  Json的特性和在數(shù)據(jù)交互中的地位就不用說(shuō)了,直接看案例。

  首先在android studio中創(chuàng)建assets文件目錄,用于存放Json數(shù)據(jù)文件,android studio 1.3 默認(rèn)項(xiàng)目文件目錄下是沒(méi)有assets文件夾的,

  所以需要我們進(jìn)行創(chuàng)建,創(chuàng)建方法如下:

     

  創(chuàng)建好assets文件目錄以后,在其目錄下創(chuàng)建一個(gè)Text.json文件。

二:如何獲得assets文件目錄下的Json數(shù)據(jù):

  在eclipse下是:InputStreamReader(getAssets().open("Text.json"),"UTF-8");獲得該文件數(shù)據(jù),并以InputStream返回?cái)?shù)據(jù)。

  而在android studio則是通過(guò):JsonLearn.this.getClass().getClassLoader().getResourceAsStream("assets/" + "Text.json");返回相應(yīng)InputStream.

三:案例展示:

  1:案例項(xiàng)目app界面如下,通過(guò)按鈕分別實(shí)現(xiàn)Json數(shù)據(jù)的讀取和創(chuàng)建,并展示在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數(shù)據(jù)*/
     findViewById(R.id.readJsioBtn).setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
         /*獲取到assets文件下的TExt.json文件的數(shù)據(jù),并以輸出流形式返回。*/
         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();
         }
       }
     });
     /*創(chuàng)建Json數(shù)據(jù)并顯示*/
     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();
         }
       }
     });
   }
 }

以上是通過(guò)Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法,希望能夠幫助到大家,在實(shí)際的項(xiàng)目開(kāi)發(fā)中可以通過(guò)Gson(谷歌)Fast-Json(阿里巴巴)這兩款Json處理API。

相關(guān)文章

最新評(píng)論