Android-封装JSON数据(JSON对象/JSON数组)

时间:2021-06-08 15:58:25

Android-封装JSON数据(JSON对象/JSON数组),一般情况下不会在Android端封装JSON的数据,因为封装JSON的数据是在服务器端进行封装了,Android更多的工作是解析(JSON对象/JSON数组)

而且在服务端封装JSON会更加简单灵活:

  例如:JsonTools.createJsonString("persons", list);/JsonTools.createJsonString("person", person);

而这篇博客是讲解在Android-封装JSON数据(JSON对象/JSON数组),只为熟悉JSON数据格式,真实开发中更多的是去解析JSON数据(JSON对象/JSON数组)


注意:⚠ 千万不要jsonObject.toString()
否则会 在前面加" 后面也加" , 都在json数据有问题

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();
}
}
}

布局代码:

<?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>

结果;

/data/data/liudeli.mynetwork01/files/pottingJSON1

{
"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/pottingJSONArray1

[
{
"name":"君君",
"age":89,
"sex":"男"
},
{
"name":"小君",
"age":99,
"sex":"女"
},
{
"name":"大君",
"age":88,
"sex":"男"
}
]

/data/data/liudeli.mynetwork01/files/pottingJSONArray2

{
"person":[
{
"name":"君君",
"age":89,
"sex":"男"
},
{
"name":"小君",
"age":99,
"sex":"女"
},
{
"name":"大君",
"age":88,
"sex":"男"
}
]
}