最近写程序遇到一个问题,把一些数据转换成json格式的字符串保存起来,这些数据有普通的键值对,还有列表类型的,下面写了一个小例子,列表数据以复选框CheckBox形式来展示,另外模拟加了一个普通数据的字段,当点击提交按钮时将EditText和选中的复选框的内容拼接成一个json字符串,效果如下图所示:
实现源码:
主要功能类MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private CheckBox cb1, cb2, cb3, cb4, cb5;
private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
private Button btn_submit;
private TextView tv_jsonString;
private EditText et_workMode;
private JSONObject object = null;
private JSONObject object2 = null;
private JSONArray jsonArray = null;
private String jsonString = "";
private List<CheckBoxInfo> checkBoxInfoList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
cb5 = (CheckBox) findViewById(R.id.cb5);
checkBoxList.add(cb1);
checkBoxList.add(cb2);
checkBoxList.add(cb3);
checkBoxList.add(cb4);
checkBoxList.add(cb5);
et_workMode = (EditText) findViewById(R.id.et_workMode);
btn_submit = (Button) findViewById(R.id.btn_submit);
tv_jsonString = (TextView) findViewById(R.id.tv_jsonString);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBoxInfoList = new ArrayList<CheckBoxInfo>();
for (CheckBox checkbox : checkBoxList) {
if (checkbox.isChecked()) {
CheckBoxInfo checkBoxInfo = new CheckBoxInfo();
checkBoxInfo.setLike(checkbox.getText().toString());
checkBoxInfoList.add(checkBoxInfo);
}
}
changeArrayDateToJson();
}
});
}
private void changeArrayDateToJson() {
jsonArray = new JSONArray();
object = new JSONObject();
for (int i = 0; i < checkBoxInfoList.size(); i++) {
object2 = new JSONObject();
try {
object2.put("like", checkBoxInfoList.get(i).getLike());
jsonArray.put(object2);
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
object.put("likeList", jsonArray);
object.put("workMode", et_workMode.getText().toString());
} catch (JSONException e) {
e.printStackTrace();
}
jsonString = object.toString();
tv_j`
onString.setText(jsonString);
Log.e("tag", "转换成json字符串: " + jsonString);
}
}
XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_workMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="工作模式"/>
<EditText
android:id="@+id/et_workMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_workMode"
android:layout_marginLeft="10dp"
android:hint="请填写工作模式"
android:textSize="14sp"/>
</RelativeLayout>
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"/>
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美术"/>
<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英语"/>
<CheckBox
android:id="@+id/cb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="语文"/>
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
<TextView
android:id="@+id/tv_jsonString"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="jsonString:"/>
</LinearLayout>
两个实体类
SettingDataInfo
package com.junto.splicingjson;
import java.io.Serializable;
import java.util.List;
/**
* Created by WangJinyong on 2017/9/20.
*/
public class SettingDataInfo implements Serializable {
private String workMode;
private List<CheckBoxInfo> checkBoxInfoList;
public String getWorkMode() {
return workMode;
}
public void setWorkMode(String workMode) {
this.workMode = workMode;
}
public List<CheckBoxInfo> getCheckBoxInfoList() {
return checkBoxInfoList;
}
public void setCheckBoxInfoList(List<CheckBoxInfo> checkBoxInfoList) {
this.checkBoxInfoList = checkBoxInfoList;
}
}
CheckBoxInfo 存放CheckBox列表
package com.junto.splicingjson;
import java.io.Serializable;
/**
* Created by WangJinyong on 2017/9/20.
*/
public class CheckBoxInfo implements Serializable {
private String like;
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
}
以上内容即可实现把数据转换成json格式的字符串
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)