jsonarray用法,json的4种解析方式
-- 1 Android SDK自带的org.json解析 --
1.1 解析原理
基于文档驱动,需要把全部文件读入到内存中,然后遍历所有数据,根据需要检索想要的数据。
1.2 相关类
JSONObject
JSONArray
JSONTokener
JSONStringer
JSONException
public Object nextValue() throws JSONException { int c = nextCleanInternal(); switch (c) { case -1: throw syntaxError("End of input"); case '{': return readObject(); case '[': return readArray(); case '\'': case '"': return nextString((char) c); default: pos--; return readLiteral(); }}
1.3 示例代码
public class OrgJsonUtil { /** * 生成Json */ public static void createJson(Context context) { try { File file = new File(context.getFilesDir(), "orgjson.json"); //实例化一个JSONObject JSONObject student = new JSONObject(); //向对象中添加数据 student.put("name", "Musk"); student.put("sex", "男"); student.put("age", 50); JSONObject course1 = new JSONObject(); course1.put("name", "数学"); course1.put("score", 98.2f); JSONObject course2 = new JSONObject(); course2.put("name", "语文"); course2.put("score", 99); //实例化一个JSONArray 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(); Log.d("TAG", "createJson: " + student); Toast.makeText(context, "Json创建成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } /** * 解析Json */ public static void parseJson(Context context) { try { //读数据 File file = new File(context.getFilesDir(), "orgjson.json"); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String line; StringBuffer sb = new StringBuffer(); while (null != (line = br.readLine())) { sb.append(line); } fis.close(); isr.close(); br.close(); Student student = new Student(); JSONObject studentJsonObject = new JSONObject(sb.toString()); //获取对象 //使用optString在获取不到对应值的时候会返回空字符串"",而使用getString会异常 String name = studentJsonObject.optString("name", ""); String sex = studentJsonObject.optString("sex", "女"); int age = studentJsonObject.optInt("age", 18); student.setName(name); student.setSex(sex); student.setAge(age); //获取数组 List<Course> courses = new ArrayList<>(); JSONArray coursesJsonArray = studentJsonObject.optJSONArray("courses"); if (coursesJsonArray != null && coursesJsonArray.length() > 0) { for (int i = 0; i < coursesJsonArray.length(); i++) { JSONObject courseJsonObject = coursesJsonArray.optJSONObject(i); Course course = new Course(); String courseName = courseJsonObject.optString("name", "学科"); float score = (float) courseJsonObject.optDouble("score", 0); course.setName(courseName); course.setScore(score); courses.add(course); } } student.setCourses(courses); Log.d("TAG", "parseJson: " + student); Toast.makeText(context, "Json解析成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } }}
本文地址:百科问答频道 https://www.neebe.cn/wenda/903299.html,易企推百科一个免费的知识分享平台,本站部分文章来网络分享,本着互联网分享的精神,如有涉及到您的权益,请联系我们删除,谢谢!