创建日期:公元2022年02月10日
修改日期:公元2022年[05月24日;08月01日]
文章状态:已完结
开发平台:Android Studio、eclipse等
适用于:Java、安卓开发
———————————————————————————————————————————
目录
1.介绍:
包括JSONObject和JSONArray两种类型
2.实例背景:
一个班级的情况:教师2个(姓名、性别、年龄、是否退休)+学生3个(姓名、性别、年龄、是否毕业)
3.如何创建、解析JSON:
package csdn;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class learnJSON {
public static void main(String[] args) {
// TODO 自动生成的方法存根
//创建:
String string="";
JSONObject jsonObject=new JSONObject();//创建JSONObject对象
try {
JSONObject jsonObject11=new JSONObject();
jsonObject11.put("name", "Lee叉叉");//放入String数据-可含中文
jsonObject11.put("sex", "man");
jsonObject11.put("year", 40);//放入int数据
jsonObject11.put("retired", false);//放入boolean数据
JSONObject jsonObject12=new JSONObject();
jsonObject12.put("name", "王mm");
jsonObject12.put("sex", "woman");
jsonObject12.put("year", 70);
jsonObject12.put("retired", true);
JSONArray jsonArray1=new JSONArray();//创建JSONArray对象
jsonArray1.put(jsonObject11);//放入数据
jsonArray1.put(jsonObject12);
JSONObject jsonObject21=new JSONObject();
JSONObject jsonObject22=new JSONObject();
JSONObject jsonObject23=new JSONObject();
jsonObject21.put("name", "小明");
jsonObject21.put("sex", "男");
jsonObject21.put("year", 22);
jsonObject21.put("graduated", true);
jsonObject22.put("name", "小红");
jsonObject22.put("sex", "女");
jsonObject22.put("year", 20);
jsonObject22.put("graduated", false);
jsonObject23.put("name", "小迪");
jsonObject23.put("sex", "男");
jsonObject23.put("year", 18);
jsonObject23.put("graduated", false);
JSONArray jsonArray2=new JSONArray();
jsonArray2.put(jsonObject21);
jsonArray2.put(jsonObject22);
jsonArray2.put(jsonObject23);
jsonObject.put("teacher", jsonArray1);
jsonObject.put("student", jsonArray2);
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
string=jsonObject.toString();
System.out.print("班级情况:\n"+string+"\n");
//解析:
try {
JSONObject jsonObjectx=new JSONObject(string);//创建JSONObject对象
JSONArray jsonArray1x=jsonObjectx.getJSONArray("teacher");//获取teacher数组
System.out.println("teacher信息:");
for (int i = 0; i < jsonArray1x.length(); i++) {
//循环解析teacher数组中的各个对象
JSONObject jsonObjectx2=jsonArray1x.getJSONObject(i);
String name=jsonObjectx2.getString("name");
String sex=jsonObjectx2.getString("sex");
int year=jsonObjectx2.getInt("year");
boolean retired=jsonObjectx2.getBoolean("retired");
String rString="未退休";
if (retired) {
rString="已退休";
}
System.out.println(name+"-"+sex+"-"+year+"-"+rString);
}
JSONArray jsonArray2x=jsonObjectx.getJSONArray("student");//获取student数组
System.out.println("student信息:");
for (int i = 0; i < jsonArray2x.length(); i++) {
JSONObject jsonObjectx3=jsonArray2x.getJSONObject(i);
String name=jsonObjectx3.getString("name");
String sex=jsonObjectx3.getString("sex");
int year=jsonObjectx3.getInt("year");
boolean graduated=jsonObjectx3.getBoolean("graduated");
String gString="未毕业";
if (graduated) {
gString="已毕业";
}
System.out.println(name+"-"+sex+"-"+year+"-"+gString);
}
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
运行结果:
班级情况:
{"teacher":[{"year":40,"sex":"man","name":"Lee叉叉","retired":false},{"year":70,"sex":"woman","name":"王mm","retired":true}],"student":[{"year":22,"sex":"男","graduated":true,"name":"小明"},{"year":20,"sex":"女","graduated":false,"name":"小红"},{"year":18,"sex":"男","graduated":false,"name":"小迪"}]}
teacher信息:
Lee叉叉-man-40-未退休
王mm-woman-70-已退休
student信息:
小明-男-22-已毕业
小红-女-20-未毕业
小迪-男-18-未毕业
4.其它:
jsonObject.toString();//将JSON对象输出成字符串
jsonArray.toString();//将JSON数组输出成字符串
private void F() {
try{
//要处理的JSON内容:即如何解析JSON数据
}
catch(JSONException e){
e.printStackTrace();
}
}
———————————————————————————————————————————
——原创!转发请注明。