解析原理
基于文件驱动,需要把全部文件读入到内存中,然后遍历所有数据,根据需要检索想要的数据
代码实例
public class OrgJson {
static class Student{
private String name;
private String sex;
private int age;
public List<Course> getList() {
return list;
}
public void setList(List<Course> list) {
this.list = list;
}
private List<Course> list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
static class Course{
private String name;
private float score;
public Course() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "Course{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
}
}
/**
* 生成一个json字符串
* @param context
*/
public static void createJson(Context context) throws Exception {
//获取json文件
File file=new File(context.getCacheDir(),"orgjson.json");
JSONObject student=new JSONObject();
student.put("name","orgJson");
student.put("sax","男");
student.put("age",23);
JSONObject course1=new JSONObject();
course1.put("name","语文");
course1.put("score",98.5f);
JSONObject course2=new JSONObject();
course2.put("name","数学");
course2.put("score",95.5f);
//将course1,course2放入数组中
JSONArray courses=new JSONArray();
courses.put(0,course1);
courses.put(1,course2);
student.put("courses",courses);
FileOutputStream fos=new FileOutputStream(file);
fos.write(student.toString().getBytes());
fos.close();
System.out.println("createJson:"+student.toString());
}
/**
* 解析json
* @param context
*/
public static void parseJson(Context context) throws Exception {
File file=new File(context.getCacheDir(),"orgjson.json");
FileInputStream fos=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fos);
BufferedReader br=new BufferedReader(isr);
StringBuffer stringBuffer=new StringBuffer();
String line;
while(null!=(line=br.readLine())){
stringBuffer.append(line);
}
fos.close();
isr.close();
br.close();
System.out.println("原始json数据:"+stringBuffer.toString());
Student bean=new Student();
JSONObject jsonObject=new JSONObject(stringBuffer.toString());
String name=jsonObject.optString("name","");
String sex=jsonObject.optString("sex","男");
int age=jsonObject.optInt("age",18);
bean.setName(name);
bean.setSex(sex);
bean.setAge(age);
//getString 若没有获取到对应字段的值时会抛错
//optString 若没有获取到对应字段的值是会返回""
List<Student.Course> list=new ArrayList<>();
JSONArray array = jsonObject.optJSONArray("courses");
for (int i=0;i<array.length();i++){
JSONObject object=array.getJSONObject(i);
String name1=object.optString("name","");
double score1=object.optDouble("score",0.0);
Student.Course course=new Student.Course();
course.setName(name1);
course.setScore((float) score1);
list.add(course);
}
bean.setList(list);
System.out.println("parseJson:"+bean.toString());
}
}