目录

  • 第1章 Android UI设计
    • ● 版本与程序
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例1-1 线性布局应用示例
    • ● 例1-2 帧布局应用示例
    • ● 例1-3 表格布局应用示例
    • ● 例1-4 相对布局应用示例
    • ● 例1-5 网格布局应用示例
    • ● 例1-6 约束布局应用示例
    • ● 例1-7 文本标签组件示例
    • ● 例1-8 制作登陆界面
    • ● 例1-9 进度条ProgressBar应用示例
    • ● 例1-10 单选按钮与复选按钮
    • ● 例1-11 简单列表示例
    • ● 例1-12 ListActivity示例
    • ● 例1-13 带有图片的列表
    • ● 例1-14 没有数据的ListView
    • ● 例1-15 改进的列表
    • ● 实战演练——生肖背后的故事
  • 第2章 Activity与多个用户界面
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例2-1 从Activity启动另一个Activity示例
    • ● 例2-2 传递数据到第二个Activity示例
    • ● 例2-3 消息提示Toast示例
    • ● 例2-4 消息对话框示例
    • ● 例2-5 选项菜单应用示例
    • ● 例2-6 上下文菜单应用示例
    • ● 实战演练——BMI体质指数计算器
  • 第3章 多媒体播放与录制
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例3-1 音乐播放器, 播放项目资源中的音乐
    • ● 例3-2 音乐播放器, 播放SD卡中的音乐
    • ● 例3-3 应用MediaPlayer设计视频播放器
    • ● 例3-4 应用VidioView设计视频播放器
    • ● 例3-5 录音示例
    • ● 例3-6 拍照示例
    • ● 实战演练——音乐播放器
  • 第4章    广播与服务
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例4-1 消息广播程序示例
    • ● 例4-2 系统通知服务示例
    • ● 例4-3 广播和时钟服务示例
    • ● 例4-4 调用系统功能拨打电话
    • ● 例4-5 后台音乐服务示例
    • ● 实战演练——播放后台音乐
  • 第5章    数据存储
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例5-1 内部存储文件示例
    • ● 例5-2 SD文件示例
    • ● 例5-3 JSON数据示例
    • ● 例5-4 SharedPreferences示例
    • ● 例5-5 创建与删除数据库示例
    • ● 例5-6 数据库记录的操作示例
    • ● 实战演练——掌上日记本
  • 第6章    图像和动画
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例6-1 绘制几何图形示例
    • ● 例6-2 补间动画示例
    • ● 例6-3 属性动画示例
    • ● 例6-4 ImageView图像浏览示例
    • ● 例6-5 ImageSwitcher展示相册示例
    • ● 例6-6 GridView展示相册示例
    • ● 例6-7 游戏中触屏事件示例
    • ● 实战演练——图片与动画
  • 第7章    网络编程
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例7-1 应用WebView的对象浏览网页
    • ● 例7-2 从Web服务器读取图像文件
    • ● 例7-3 读取JSON数据
    • ● 例7-4 解析JSON数据
    • ● 实战演练——城市天气预报
  • 第8章    分享美食
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章知识点讲解
    • ● 项目制作流程演示
    • ● 示范1 图标SVG
    • ● 示范2 卡片视图
    • ● 示范3 导航视图
  • 课程资源
    • ● 学生资源包
    • ● 网站地址
例5-3 JSON数据示例

【例5-3】 解析JSON格式数据示例

这里设计三种示例:解析单个json数据,解析多个json数据,解析json文件。

 


JSON是一种轻量级的数据交换格式,制作一个文本文件weather.json放到assets目录。文件weather.json内容如下:

{"city":"深圳","date":"15日星期日","high":"高温 29℃","fx":"无持续风向","low":"低温 25℃","type":"中雨"}

教学视频


布局文件 activity_json.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"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"

    android:orientation="vertical"

    android:weightSum="1">


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">


        <TextView

            android:id="@+id/textView"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginLeft="20dp"

            android:layout_marginTop="2dp"

            android:text="城市"

            android:textSize="24sp" />


        <EditText

            android:id="@+id/editText"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:inputType="textPersonName"

            android:textSize="24sp" />


    </LinearLayout>


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">


        <TextView

            android:id="@+id/textView2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginLeft="20dp"

            android:layout_marginTop="2dp"

            android:text="气温"

            android:textSize="24sp" />


        <EditText

            android:id="@+id/editText2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:inputType="textPersonName"

            android:textSize="24sp" />

    </LinearLayout>


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">


        <Button

            android:id="@+id/jsonBtn"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginLeft="20dp"

            android:layout_marginTop="2dp"

            android:text="解析单个json数据"

            android:textSize="24sp" />

    </LinearLayout>


    <ImageView

        android:id="@+id/imageView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        app:srcCompat="@drawable/logo" />


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">


        <TextView

            android:id="@+id/txt3"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:textSize="24sp" />

    </LinearLayout>


    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="vertical">


        <Button

            android:id="@+id/arrayBtn"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginLeft="20dp"

            android:layout_marginTop="2dp"

            android:text="解析多个json数据"

            android:textSize="24sp" />


        <Button

            android:id="@+id/fileBtn"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginLeft="20dp"

            android:layout_marginTop="2dp"

            android:text="解析json文件"

            android:textSize="24sp" />

    </LinearLayout>


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">


        <TextView

            android:id="@+id/txt4"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:textSize="24sp" />

    </LinearLayout>

</LinearLayout>



控制文件JsonActivity.java源代码

package com.myapp.chap05;


import androidx.appcompat.app.AppCompatActivity;


import android.content.res.AssetManager;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;


import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;


import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;


public class JsonActivity extends AppCompatActivity implements View.OnClickListener {

    EditText editText,editText2;

    TextView txt3,txt4;

    Button jsonBtn, arrayBtn, fileBtn;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_json);

        editText = (EditText) findViewById(R.id.editText);

        editText2 = (EditText) findViewById(R.id.editText2);

        txt3 = (TextView) findViewById(R.id.txt3);

        txt4 = (TextView) findViewById(R.id.txt4);

        jsonBtn = (Button) findViewById(R.id.jsonBtn);

        arrayBtn = (Button) findViewById(R.id.arrayBtn);

        fileBtn = (Button) findViewById(R.id.fileBtn);

        jsonBtn.setOnClickListener(this);

        arrayBtn.setOnClickListener(this);

        fileBtn.setOnClickListener(this);

    }

    @Override

    public void onClick(View view) {

        if(view == jsonBtn){

            setJsonData();

        }else if(view == arrayBtn){

            setArrayData();

        }else if(view == fileBtn){

            getJsonFile();

        }

    }

    //解析单个JSON对象

    private void setJsonData(){

        try {

            JSONObject test = new JSONObject();

            test.put("城市", "深圳");

            test.put("气温", 30);

            String cs = test.getString("城市");

            int qw = test.getInt("气温");

            editText.setText(cs);

            editText2.setText(Integer.toString(qw));

        }catch (JSONException e){e.printStackTrace();}

    }

    //解析JSON数组

    private void setArrayData(){

        JSONObject p1,p2,p3;

        JSONArray weather;

        try{

            //注意,英文的冒号和逗号是语法,中文的逗号是普通字符

            String str1 = "{城市:成都,气温:21-28度,小雨}";

            String str2 = "{城市:大理,气温:0-14度,多云}";

            String str3 = "{城市:拉萨,气温:-9-5度,多云}";

            //3个JSON对象和1个JSON数组

            p1=new JSONObject(str1);

            p2=new JSONObject(str2);

            p3=new JSONObject(str3);

            weather = new JSONArray();

            weather.put(p1);

            weather.put(p2);

            weather.put(p3);

            String jc,jw;

            int length = weather.length();

            for(int i=0; i<length; i++){    //遍历JSON数组

                JSONObject jsonObject = weather.getJSONObject(i);

                jc = jsonObject.getString("城市") + ":";

                jw =  jsonObject.getString("气温") + "\n";

                txt3.append(jc+jw);

            }

        }catch (JSONException e){e.printStackTrace();}

    }

    //解析JSON文件

    private void getJsonFile(){

        AssetManager assetManager = getAssets();//获得assets资源管理器

        String strs="";

        String fileName="weather.json";//要解析的JSON文件

        try {

            InputStreamReader reader = new InputStreamReader(

                    assetManager.open(fileName),"utf-8");

            BufferedReader buffer = new BufferedReader(reader);

            String line;

            StringBuilder stringBuilder = new StringBuilder(); //可变的字符序列

            while((line=buffer.readLine())!=null){

                stringBuilder.append(line);

            }

            strs = stringBuilder.toString();

            buffer.close();

            reader.close();

            JSONObject jsonObject = new JSONObject(strs);

            String city, date, high, fx, low, type;

            city = jsonObject.getString("city");

            date = jsonObject.getString("date");

            high = jsonObject.getString("high");

            fx = jsonObject.getString("fx");

            low = jsonObject.getString("low");

            type = jsonObject.getString("type");

            txt4.setText(city+":"+date+","+high+","+fx+","+low+","+type);

        }catch (IOException | JSONException e){

            txt4.append("FileNotFound");

        }

    }

}


打开项目配置文件AndroidManifest.xml,配置要启动的Activity类名


        <activity android:name=".JsonActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>