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。
- android客戶(hù)端從服務(wù)器端獲取json數(shù)據(jù)并解析的實(shí)現(xiàn)代碼
- Android中Retrofit 2.0直接使用JSON進(jìn)行數(shù)據(jù)交互
- Android中使用Gson解析JSON數(shù)據(jù)的兩種方法
- Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實(shí)例
- Android訪(fǎng)問(wèn)php取回json數(shù)據(jù)實(shí)例
- android調(diào)用國(guó)家氣象局天氣預(yù)報(bào)接口json數(shù)據(jù)格式解釋
- Android App中讀取XML與JSON格式數(shù)據(jù)的基本方法示例
- android針對(duì)json數(shù)據(jù)解析方法實(shí)例分析
- android解析JSON數(shù)據(jù)
- Android開(kāi)發(fā)使用json實(shí)現(xiàn)服務(wù)器與客戶(hù)端數(shù)據(jù)的交互功能示例
相關(guān)文章
Android Studio 多層級(jí) Module 對(duì) aar 引用問(wèn)題解決方法
這篇文章主要介紹了Android Studio 多層級(jí) Module 對(duì) aar 引用問(wèn)題的解決方法,需要的朋友參考下2017-12-12詳解Android短信的發(fā)送和廣播接收實(shí)現(xiàn)短信的監(jiān)聽(tīng)
本篇文章主要介紹了Android短信的發(fā)送和廣播接收實(shí)現(xiàn)短信的監(jiān)聽(tīng),可以實(shí)現(xiàn)短信收發(fā),有興趣的可以了解一下。2016-11-11Kotlin中Lambda表達(dá)式與高階函數(shù)使用分析講解
lambda 本質(zhì)上是可以傳遞給函數(shù)的一小段代碼,Kotlin 與 Java 中的 Lambda 有一定的區(qū)別,除了對(duì) lambda 的全面支持外,還有內(nèi)聯(lián)函數(shù)等簡(jiǎn)潔高效的特性。下面我們來(lái)仔細(xì)看一下2022-12-12Android?手寫(xiě)熱修復(fù)dex實(shí)例詳解
這篇文章主要為大家介紹了Android?手寫(xiě)熱修復(fù)dex實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android?Flutter實(shí)現(xiàn)彈簧動(dòng)畫(huà)交互的示例詳解
物理模擬可以讓?xiě)?yīng)用程序的交互感覺(jué)逼真和互動(dòng),本文章實(shí)現(xiàn)了演示了如何使用彈簧模擬將小部件從拖動(dòng)的點(diǎn)移回中心,感興趣的可以了解一下2023-04-04Android開(kāi)發(fā)簡(jiǎn)單實(shí)現(xiàn)搖動(dòng)動(dòng)畫(huà)的方法
這篇文章主要介紹了Android開(kāi)發(fā)簡(jiǎn)單實(shí)現(xiàn)搖動(dòng)動(dòng)畫(huà)的方法,結(jié)合實(shí)例形式分析了Android搖動(dòng)動(dòng)畫(huà)的布局與功能簡(jiǎn)單實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10Android實(shí)時(shí)獲取攝像頭畫(huà)面?zhèn)鬏斨罰C端思路詳解
這篇文章主要介紹了Android實(shí)時(shí)獲取攝像頭畫(huà)面?zhèn)鬏斨罰C端思路詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07