Json解析(Android SDK中自带的org.json包)

添加库文件

在这里插入图片描述
在App的Dependences选项卡里,单击 “+” 选择Library dependency在这里插入图片描述

1,若使用JSON中的数据,就需要将JSON数据解析出来。Android 上有两种解析技术可供选择,一种是通过Android 内置的org.json包,一种是通过Google的开源Gson库(需要使用gson.jar包)。

本次讲的是第一种使用org.json解析JSON数据的方法:

格式一:
{
“name”:“tom”,
“age”:18,
“sex”:“man”
}

格式二:
[18,20,30]

解析格式一:

 JSONObject jsonObject = new JSONObject(data_person);
            String name = jsonObject.optString("name");
            String age = jsonObject.optString("age");
            String sex = jsonObject.optString("sex");

解析格式二:

 JSONArray jsonArray = new JSONArray(data_age);
            for (int i = 0; i < jsonArray.length(); i++) {
              int age=jsonArray.optInt(i);
            }

做了个小demo总结了一下

在这里插入图片描述

json源文件:
[18,20,30]

{
“name”:“tom”,
“age”:18,
“sex”:“man”
}

[
{“temp”:“20’C”,“weather”:“晴天”,“name”:“北京”,“pm”:“80”,“wind”:“1级”},
{“temp”:“19’C”,“weather”:“多云”,“name”:“上海”,“pm”:“70”,“wind”:“2级”},
{“temp”:“18’C”,“weather”:“阴天”,“name”:“广州”,“pm”:“60”,“wind”:“3级”}
]

1,在raw目录下做三个.json文件存放我要解析的数据源
2,在MainActivity文件下用三个方法分别解析json文件
3,将处理后的数据set到edittext中

MainActivity代码:

package com.example.chapter_four;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    EditText editText_age, editText_person, editText_weather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText_age = (EditText) findViewById(R.id.edit_age);
        editText_person = (EditText) findViewById(R.id.edit_people);
        editText_weather = (EditText) findViewById(R.id.edit_weather);
        age();
        personl();
        weather();
    }

    private void age() {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream inputStream1 = getResources().openRawResource(R.raw.age);
        try {
            byte[] bytes = new byte[inputStream1.available()];//存
            inputStream1.read(bytes);
            String data_age = new String(bytes);//转
            JSONArray jsonArray = new JSONArray(data_age);//转
            for (int i = 0; i < jsonArray.length(); i++) {
                stringBuilder.append(jsonArray.optInt(i) + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        editText_age.setText(stringBuilder);
    }


    private void personl() {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream inputStream2 = getResources().openRawResource(R.raw.person);
        try {
            byte[] bytes = new byte[inputStream2.available()];//存
            inputStream2.read(bytes);
            String data_person = new String(bytes);//转
            JSONObject jsonObject = new JSONObject(data_person);//转
            String name = jsonObject.optString("name");
            String age = jsonObject.optString("age");
            String sex = jsonObject.optString("sex");
            stringBuilder.append(name).append(age).append(sex);

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

        editText_person.setText(stringBuilder);
    }

    private void weather() {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream inputStream3 = getResources().openRawResource(R.raw.weather);
        try {
            byte[] bytes = new byte[inputStream3.available()];//存
            inputStream3.read(bytes);
            String data_weather = new String(bytes);//转
            JSONArray jsonArray = new JSONArray(data_weather);//转
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.optJSONObject(i);//出{...}
                stringBuilder.append(jsonObject.optString("name") + "\t" + jsonObject.optString("weather") + "\t" +
                        jsonObject.optString("temp") + "\t" + jsonObject.optString("pm") +
                        "\t" + jsonObject.optString("wind") + "\n");//出“value”
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        editText_weather.setText(stringBuilder);
    }

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_cc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年龄:"
        android:textSize="24dp" />

    <EditText
        android:id="@+id/edit_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="个人信息:"
        android:textSize="24dp" />

    <EditText
        android:id="@+id/edit_people"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="天气:"
        android:textSize="24dp" />

    <EditText
        android:id="@+id/edit_weather"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值