专业汉语词典知识平台,分享汉字词语知识、历史文学知识解答!

励北网
励北网

jsonarray用法,json的4种解析方式

来源:小易整编  作者:小易  发布时间:2023-02-23 03:50
摘要:jsonarray用法,json的4种解析方式- 1AndroidSDK自带的org.json解析 -1.1解析原理基于文档驱动,需要把全部文件读入到内存中,然后遍历所有数据,根据需要检索想要的数据。1.2相关类JSONObjectJS...

jsonarray用法,json的4种解析方式

--  1 Android SDK自带的org.json解析  --

jsonarray用法,json的4种解析方式

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,易企推百科一个免费的知识分享平台,本站部分文章来网络分享,本着互联网分享的精神,如有涉及到您的权益,请联系我们删除,谢谢!


百科问答
小编:小易整编
相关文章相关阅读
  • jsonobject的用法,jsonobject的使用

    jsonobject的用法,jsonobject的使用

    jsonobject的用法,jsonobject的使用在程序开发过程中,在参数传递,函数返回值等方面,越来越多的使用JSON。JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,同时也易于机器解析和生成、...

  • json数组,json数组详解

    json数组,json数组详解

    json数组,json数组详解简单说,所谓对象,就是一种无序的数据集合,由若干个“键值对”(key-value)构成。一、数组作为JSON对象["Ford","BMW","Fiat"]在JSON中的数组,几乎和在JavaScript中数组相...

  • jsonarray用法,json的4种解析方式

    jsonarray用法,json的4种解析方式

    jsonarray用法,json的4种解析方式-- 1AndroidSDK自带的org.json解析 --1.1解析原理基于文档驱动,需要把全部文件读入到内存中,然后遍历所有数据,根据需要检索想要的数据。1.2相关类JSONObjectJS...

  • 周排行
  • 月排行
  • 年排行

精彩推荐