Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法
首先介紹下JSON的定義,JSON是JavaScript Object Notation的縮寫。
一種輕量級的數(shù)據(jù)交換格式,具有良好的可讀和便于快速編寫的特性。業(yè)內(nèi)主流技術(shù)為其提供了完整的解決方案(有點(diǎn)類似于正則表達(dá)式,獲得了當(dāng)今大部分語言的支持),從而可以在不同平臺間進(jìn)行數(shù)據(jù)交換。JSON采用兼容性很高的文本格式,同時也具備類似于C語言體系的行為。
JSON的結(jié)構(gòu):
(1) Name/Value Pairs(無序的):類似所熟知的Keyed list、 Hash table、Disctionary和Associative array。在Android平臺中同時存在另外一個類 "Bundle",某種程度上具有相似的行為。
(2) Array(有序的):一組有序的數(shù)據(jù)列表。
一: Json的特性和在數(shù)據(jù)交互中的地位就不用說了,直接看案例。
首先在android studio中創(chuàng)建assets文件目錄,用于存放Json數(shù)據(jù)文件,android studio 1.3 默認(rèn)項(xiàng)目文件目錄下是沒有assets文件夾的,
所以需要我們進(jìn)行創(chuàng)建,創(chuàng)建方法如下:

創(chuàng)建好assets文件目錄以后,在其目錄下創(chuàng)建一個Text.json文件。
二:如何獲得assets文件目錄下的Json數(shù)據(jù):
在eclipse下是:InputStreamReader(getAssets().open("Text.json"),"UTF-8");獲得該文件數(shù)據(jù),并以InputStream返回?cái)?shù)據(jù)。
而在android studio則是通過:JsonLearn.this.getClass().getClassLoader().getResourceAsStream("assets/" + "Text.json");返回相應(yīng)InputStream.
三:案例展示:
1:案例項(xiàng)目app界面如下,通過按鈕分別實(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();
}
}
});
}
}
以上是通過Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法,希望能夠幫助到大家,在實(shí)際的項(xiàng)目開發(fā)中可以通過Gson(谷歌)Fast-Json(阿里巴巴)這兩款Json處理API。
- android客戶端從服務(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訪問php取回json數(shù)據(jù)實(shí)例
- android調(diào)用國家氣象局天氣預(yù)報接口json數(shù)據(jù)格式解釋
- Android App中讀取XML與JSON格式數(shù)據(jù)的基本方法示例
- android針對json數(shù)據(jù)解析方法實(shí)例分析
- android解析JSON數(shù)據(jù)
- Android開發(fā)使用json實(shí)現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能示例
相關(guān)文章
Android Studio 多層級 Module 對 aar 引用問題解決方法
這篇文章主要介紹了Android Studio 多層級 Module 對 aar 引用問題的解決方法,需要的朋友參考下2017-12-12
詳解Android短信的發(fā)送和廣播接收實(shí)現(xiàn)短信的監(jiān)聽
本篇文章主要介紹了Android短信的發(fā)送和廣播接收實(shí)現(xiàn)短信的監(jiān)聽,可以實(shí)現(xiàn)短信收發(fā),有興趣的可以了解一下。2016-11-11
Kotlin中Lambda表達(dá)式與高階函數(shù)使用分析講解
lambda 本質(zhì)上是可以傳遞給函數(shù)的一小段代碼,Kotlin 與 Java 中的 Lambda 有一定的區(qū)別,除了對 lambda 的全面支持外,還有內(nèi)聯(lián)函數(shù)等簡潔高效的特性。下面我們來仔細(xì)看一下2022-12-12
Android?Flutter實(shí)現(xiàn)彈簧動畫交互的示例詳解
物理模擬可以讓應(yīng)用程序的交互感覺逼真和互動,本文章實(shí)現(xiàn)了演示了如何使用彈簧模擬將小部件從拖動的點(diǎn)移回中心,感興趣的可以了解一下2023-04-04
Android開發(fā)簡單實(shí)現(xiàn)搖動動畫的方法
這篇文章主要介紹了Android開發(fā)簡單實(shí)現(xiàn)搖動動畫的方法,結(jié)合實(shí)例形式分析了Android搖動動畫的布局與功能簡單實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
Android實(shí)時獲取攝像頭畫面?zhèn)鬏斨罰C端思路詳解
這篇文章主要介紹了Android實(shí)時獲取攝像頭畫面?zhèn)鬏斨罰C端思路詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

