Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法
首先介紹下JSON的定義,JSON是JavaScript Object Notation的縮寫。
一種輕量級(jí)的數(shù)據(jù)交換格式,具有良好的可讀和便于快速編寫的特性。業(yè)內(nèi)主流技術(shù)為其提供了完整的解決方案(有點(diǎn)類似于正則表達(dá)式,獲得了當(dāng)今大部分語言的支持),從而可以在不同平臺(tái)間進(jìn)行數(shù)據(jù)交換。JSON采用兼容性很高的文本格式,同時(shí)也具備類似于C語言體系的行為。
JSON的結(jié)構(gòu):
(1) Name/Value Pairs(無序的):類似所熟知的Keyed list、 Hash table、Disctionary和Associative array。在Android平臺(tái)中同時(shí)存在另外一個(gè)類 "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)建一個(gè)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)用國(guó)家氣象局天氣預(yù)報(bào)接口json數(shù)據(jù)格式解釋
- Android App中讀取XML與JSON格式數(shù)據(jù)的基本方法示例
- android針對(duì)json數(shù)據(jù)解析方法實(shí)例分析
- android解析JSON數(shù)據(jù)
- Android開發(fā)使用json實(shí)現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能示例
相關(guān)文章
Android使用ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除和拖拽
這篇文章主要為大家詳細(xì)介紹了Android使用ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除和拖拽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android 媒體開發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析
這篇文章主要介紹了Android 媒體開發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析,需要的朋友可以參考下2017-08-08
Android DrawerLayout實(shí)現(xiàn)側(cè)拉菜單功能
這篇文章主要介紹了Android DrawerLayout實(shí)現(xiàn)側(cè)拉菜單功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
Android不顯示開機(jī)向?qū)Ш烷_機(jī)氣泡問題
這篇文章主要介紹了Android不顯示開機(jī)向?qū)Ш烷_機(jī)氣泡問題,需要的朋友可以參考下2019-05-05
Android自定義ViewGroup實(shí)現(xiàn)淘寶商品詳情頁
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)淘寶商品詳情頁,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Android使用Jni實(shí)現(xiàn)壓力鍋數(shù)據(jù)檢測(cè)效果示例
這篇文章主要介紹了Android使用Jni實(shí)現(xiàn)壓力鍋數(shù)據(jù)檢測(cè)效果,涉及Android結(jié)合Jni實(shí)現(xiàn)進(jìn)度條模擬壓力鍋數(shù)據(jù)監(jiān)測(cè)效果的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
android編程實(shí)現(xiàn)對(duì)話框的封裝實(shí)例
這篇文章主要介紹了android編程實(shí)現(xiàn)對(duì)話框的封裝,以實(shí)例形式分析了Android針對(duì)對(duì)話框的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android獲取apk簽名指紋的md5值(防止重新被打包)的實(shí)現(xiàn)方法
這篇文章主要介紹了Android獲取apk簽名指紋的md5值以防止重新被打包的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android獲取apk md5值的常用技巧,需要的朋友可以參考下2016-07-07

