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)彈出菜單標(biāo)記已讀、未讀消息
這篇文章主要介紹了Android仿QQ滑動(dòng)彈出菜單標(biāo)記已讀、未讀消息的相關(guān)資料,需要的朋友可以參考下2016-01-01
Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程詳解
這篇文章主要介紹了Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-05-05
手把手教你實(shí)現(xiàn)Android編譯期注解
今天給大家介紹Android編譯期注解sdk的步驟以及注意事項(xiàng),并簡(jiǎn)要分析了運(yùn)行時(shí)注解以及字節(jié)碼技術(shù)在生成代碼上與編譯期注解的不同與優(yōu)劣,感興趣的朋友一起看看吧2021-07-07
android文件存儲(chǔ)和SharedPreferences存儲(chǔ)的項(xiàng)目實(shí)例
本文主要介紹了android文件存儲(chǔ)和SharedPreferences存儲(chǔ)的項(xiàng)目實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
這篇文章將介紹如何定制當(dāng)今主流的對(duì)話框,通過(guò)自定義dialog實(shí)現(xiàn)加載對(duì)話框效果,具體實(shí)現(xiàn)代碼大家通過(guò)本文學(xué)習(xí)吧2018-05-05

