package liudeli.mynetwork01; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.View; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.FileOutputStream; public class MainActivity extends Activity { private final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 封裝JSON對象數據 * { * "name":"李四", * "age":99, * "hobby":"愛好是練習截拳道" * } * * @param view */ public void pottingJSON1(View view) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("name", "李四"); jsonObject.put("age", 99); jsonObject.put("hobby", "愛好是練習截拳道"); } catch (JSONException e) { e.printStackTrace(); } finally { Log.d(TAG, "jsonObject:" + jsonObject); saveJSONDataToFile("pottingJSON1", jsonObject); } } /** * 封裝JSON對象-帶Key(student) * { * "Student":{ * "name":"李四", * "age":99, * "hobby":"愛好是練習截拳道" * } * } * * @param view */ public void pottingJSON2(View view) { JSONObject jsonObjectALL = null; try { // student json 對象 JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "李四"); jsonObject.put("age", 99); jsonObject.put("hobby", "愛好是練習截拳道"); // 整個最大的 json 對象 jsonObjectALL = new JSONObject(); /** * 注意:⚠ 千萬不要jsonObject.toString() * 不然會 在前面加" 後面也加" , 都在json數據有問題 */ jsonObjectALL.put("student", jsonObject); } catch (JSONException e) { e.printStackTrace(); } finally { Log.d(TAG, "jsonObjectALL:" + jsonObjectALL); saveJSONDataToFile("pottingJSON2", jsonObjectALL); } } /** * 封裝JSON對象-嵌套對象 * { * "student":{ * "name":"李四", * "age":99, * "hobby":"愛好是練習截拳道", * "dog":{ * "name":"阿黃", * "age":"77", * "sex":"母" * } * } * } * @param view */ public void pottingJSON3(View view) { JSONObject jsonObjectALL = null; try { // dog json 對象 JSONObject dogJSONObject = new JSONObject(); dogJSONObject.put("name", "阿黃"); dogJSONObject.put("age", 77); dogJSONObject.put("sex", "母"); // student json 對象 JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "李四"); jsonObject.put("age", 99); jsonObject.put("hobby", "愛好是練習截拳道"); /** * 注意:⚠ 千萬不要dogJSONObject.toString() * 不然會 在前面加" 後面也加" , 都在json數據有問題 */ jsonObject.put("dog", dogJSONObject); // 整個最大的 json 對象 jsonObjectALL = new JSONObject(); /** * 注意:⚠ 千萬不要jsonObject.toString() * 不然會 在前面加" 後面也加" , 都在json數據有問題 */ jsonObjectALL.put("student", jsonObject); } catch (JSONException e) { e.printStackTrace(); } finally { Log.d(TAG, "jsonObjectALL:" + jsonObjectALL); saveJSONDataToFile("pottingJSON3", jsonObjectALL); } } /** * 封裝JSON數組 * [ * { * "name":"君君", * "age":89, * "sex":"男" * }, * { * "name":"小君", * "age":99, * "sex":"女" * }, * { * "name":"大君", * "age":88, * "sex":"男" * } * ] */ public void pottingJSONArray1(View view) { try { // 第一個JSON對象 JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "君君"); jsonObject.put("age", 89); jsonObject.put("sex", "男"); // 第二個JSON對象 JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("name", "小君"); jsonObject2.put("age", 99); jsonObject2.put("sex", "女"); // 第三個JSON對象 JSONObject jsonObject3 = new JSONObject(); jsonObject3.put("name", "大君"); jsonObject3.put("age", 88); jsonObject3.put("sex", "男"); // 定義個JSON數組,把上面的三個JSON對象裝進去 JSONArray jsonArray = new JSONArray(); jsonArray.put(0, jsonObject); jsonArray.put(1, jsonObject2); jsonArray.put(2, jsonObject3); Log.d(TAG, "jsonArray:" + jsonArray); saveJSONDataToFile("pottingJSONArray1", jsonArray); } catch (Exception e) { e.printStackTrace(); } } /** * 封裝JSON數組-帶Key * { * "person":[ * { * "name":"君君", * "age":89, * "sex":"男" * }, * { * "name":"小君", * "age":99, * "sex":"女" * }, * { * "name":"大君", * "age":88, * "sex":"男" * } * ] * } * @param view */ public void pottingJSONArray2(View view) { try { // 第一個JSON對象 JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "君君"); jsonObject.put("age", 89); jsonObject.put("sex", "男"); // 第二個JSON對象 JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("name", "小君"); jsonObject2.put("age", 99); jsonObject2.put("sex", "女"); // 第三個JSON對象 JSONObject jsonObject3 = new JSONObject(); jsonObject3.put("name", "大君"); jsonObject3.put("age", 88); jsonObject3.put("sex", "男"); // 定義個JSON數組,把上面的三個JSON對象裝進去 JSONArray jsonArray = new JSONArray(); jsonArray.put(0, jsonObject); jsonArray.put(1, jsonObject2); jsonArray.put(2, jsonObject3); // 整個最大的 json 對象 JSONObject jsonObjectAll = new JSONObject(); // 把上面的JSON數組,裝進去 jsonObjectAll.put("person", jsonArray); Log.d(TAG, "jsonObjectAll:" + jsonObjectAll); saveJSONDataToFile("pottingJSONArray2", jsonObjectAll); } catch (Exception e) { e.printStackTrace(); } } /** * 保存JSON數據到文件 */ private void saveJSONDataToFile(String fileName, JSONObject jsonData) { try { FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); fos.write(jsonData.toString().getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 保存JSON數據到文件 */ private void saveJSONDataToFile(String fileName, JSONArray jsonData) { try { FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); fos.write(jsonData.toString().getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }
佈局代碼:java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="封裝JSON對象" android:onClick="pottingJSON1" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="封裝JSON對象-帶Key" android:onClick="pottingJSON2" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="封裝JSON對象-嵌套對象" android:onClick="pottingJSON3" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="封裝JSON數組" android:onClick="pottingJSONArray1" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="封裝JSON數組-帶Key" android:onClick="pottingJSONArray2" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
結果;android
/data/data/liudeli.mynetwork01/files/pottingJSON1json
{
"name":"李四",
"age":99,
"hobby":"愛好是練習截拳道"
}
/data/data/liudeli.mynetwork01/files/pottingJSON2數組
{
"student":{
"name":"李四",
"age":99,
"hobby":"愛好是練習截拳道"
}
}
/data/data/liudeli.mynetwork01/files/pottingJSON3服務器
{
"student":{
"name":"李四",
"age":99,
"hobby":"愛好是練習截拳道",
"dog":{
"name":"阿黃",
"age":77,
"sex":"母"
}
}
}
/data/data/liudeli.mynetwork01/files/pottingJSONArray1app
[
{
"name":"君君",
"age":89,
"sex":"男"
},
{
"name":"小君",
"age":99,
"sex":"女"
},
{
"name":"大君",
"age":88,
"sex":"男"
}
]
/data/data/liudeli.mynetwork01/files/pottingJSONArray2ide
{
"person":[
{
"name":"君君",
"age":89,
"sex":"男"
},
{
"name":"小君",
"age":99,
"sex":"女"
},
{
"name":"大君",
"age":88,
"sex":"男"
}
]
}