Android中生成、使用Json數(shù)據(jù)實(shí)例
1、Json的制作
package com.example.usingjson2;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("\"Hello World\"");
JSONObject object = new JSONObject();
try {
object.put("one", "yu");
object.put("two", "xi");
object.put("three", "kuo");
object.put("four", "hi");
System.out.println(object);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、Json的使用
(1)首先在當(dāng)前包中創(chuàng)建新的class
package com.example.useingjson;
public class Data {
public static final String jsonStr = "{'arr':[1,2,3,4],'dat':[yu,xi,kuo]}";
}
(2)在Activity中調(diào)用
package com.example.useingjson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(Data.jsonStr);
try {
JSONObject jsonObject = new JSONObject(Data.jsonStr);
JSONArray jsonArray = jsonObject.getJSONArray("arr");
System.out.println(jsonArray);
for(int i = 0; i < jsonArray.length(); i++)
{
System.out.println(jsonArray.get(i));
}
JSONArray jsonArray2 = jsonObject.getJSONArray("dat");
System.out.println(jsonArray2);
for(int j = 0; j < jsonArray2.length(); j++)
{
System.out.println(jsonArray2.get(j));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、注意
Json的制作與使用分別是兩個(gè)項(xiàng)目,由于剛接觸Android,代碼比較簡(jiǎn)單,重在理解原理。
- android客戶端從服務(wù)器端獲取json數(shù)據(jù)并解析的實(shí)現(xiàn)代碼
- Android中使用Gson解析JSON數(shù)據(jù)的兩種方法
- Android中的JSON詳細(xì)總結(jié)
- Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實(shí)例
- Android webview與js交換JSON對(duì)象數(shù)據(jù)示例
- Android訪問(wèn)php取回json數(shù)據(jù)實(shí)例
- Android 解析JSON對(duì)象及實(shí)例說(shuō)明
- Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法
- android調(diào)用國(guó)家氣象局天氣預(yù)報(bào)接口json數(shù)據(jù)格式解釋
- Android Studio獲取網(wǎng)絡(luò)JSON數(shù)據(jù)并處理的方法
相關(guān)文章
android 仿QQ動(dòng)態(tài)背景、視頻背景的示例代碼
本篇文章主要介紹了android 仿QQ動(dòng)態(tài)背景、視頻背景的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android自定義SurfaceView實(shí)現(xiàn)畫板功能
這篇文章主要為大家詳細(xì)介紹了Android自定義SurfaceView實(shí)現(xiàn)畫板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android ListView填充數(shù)據(jù)的方法
這篇文章主要介紹了Android ListView填充數(shù)據(jù)的方法的相關(guān)資料,大多數(shù)實(shí)現(xiàn)這樣的功能都是使用XML文件,這里就說(shuō)下不使用布局文件如何實(shí)現(xiàn),需要的朋友可以參考下2017-09-09Android開(kāi)發(fā)之圖片旋轉(zhuǎn)功能實(shí)現(xiàn)方法【基于Matrix】
這篇文章主要介紹了Android開(kāi)發(fā)之圖片旋轉(zhuǎn)功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android基于matrix矩陣操作圖形變換的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09Android實(shí)現(xiàn)注冊(cè)登錄界面的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)注冊(cè)登錄界面的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05android實(shí)現(xiàn)上傳本地圖片到網(wǎng)絡(luò)功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)上傳本地圖片到網(wǎng)絡(luò)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Android 實(shí)現(xiàn)全屏顯示的幾種方法整理
這篇文章主要介紹了Android 實(shí)現(xiàn)全屏顯示的幾種方法整理的相關(guān)資料,需要的朋友可以參考下2017-03-03Android 自定義SeekBar動(dòng)態(tài)改變硬件音量大小實(shí)現(xiàn)和音量鍵的同步(推薦)
這篇文章主要介紹了 Android 自定義SeekBar動(dòng)態(tài)改變硬件音量大小實(shí)現(xiàn)和音量鍵的同步效果,整段代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01