安卓基础:JSON初识

JSON初识

一.什么是JSON

轻量级的数据交换格式

二.JSON的两种结构

同个https://www.json.cn网站可以清除的解析JSON的结构
这里写图片描述

1.JSONObject 单条JSON数据
2.JSONArray 多条JSON组合

三.如何解析JSONObject

效果图:点击按钮获取JSON中的数据并使用

这里写图片描述
这里写图片描述

 //解析jsonObj对象的方法
    private void parseJsonobj() {
    /*    {"name":"张三","age":21,"info":{"class":"三年一班","id":2016001}}*/
        String str="{\"name\":\"张三\",\"age\":21,\"info\":{\"class\":\"三年一班\",\"id\":2016001}}";
        try {
                JSONObject jsonObject2=new JSONObject(str);//创建JSON对象
                JSONObject    classjson=  jsonObject2.getJSONObject("info");//获取JSON对象中的JSON
            tv_class.setText(classjson.getString("class"));
           tv_id.setText(classjson.getInt("id")+"");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

完整代码:

package com.example.a22120.day6_json;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private Button btn_get;
    private TextView tv_name,tv_age ,tv_class,tv_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindID();
        btn_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                parseJson();
                parseJsonobj();
          /*    parseJsonArray();*/
            }
        });

    }


    private void bindID() {
        btn_get=findViewById(R.id.getname);
        tv_age=findViewById(R.id.tv_age2);
        tv_name=findViewById(R.id.tv_name2);
        btn_get= findViewById(R.id.getname);
        tv_class=findViewById(R.id.tv_class2);
        tv_id=findViewById(R.id.tv_id2);
    }

    //解析jsonObj对象的方法
    private void parseJsonobj() {
    /*    {"name":"张三","age":21,"info":{"class":"三年一班","id":2016001}}*/
        String str="{\"name\":\"张三\",\"age\":21,\"info\":{\"class\":\"三年一班\",\"id\":2016001}}";
        try {
            JSONObject jsonObject2=new JSONObject(str);
          JSONObject    classjson=  jsonObject2.getJSONObject("info");
            tv_class.setText(classjson.getString("class"));
           tv_id.setText(classjson.getInt("id")+"");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    //ctrl+r 是替换的快捷键
    //解析JSON的方法
    private void parseJson() {
        String json_srt=" {\"name\":\"张三\",\"age\":21}";    //“\”为转义符
        try {
            JSONObject jsonObject=new JSONObject(json_srt);
            String name=jsonObject.getString("name");
            int age=jsonObject.getInt("age");
            tv_name.setText(name);
            tv_age.setText(Integer.toString(age)); //或者直接 用  age+""

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


}

UI代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.a22120.day6_json.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="名字:"
        android:id="@+id/tv_name"
        />
    <TextView
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/tv_name2"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/tv_age"
        android:text="年龄:"
        />
    <TextView
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/tv_age2"
        />
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:text="班级:"
            android:id="@+id/tv_class"
            />
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:id="@+id/tv_class2"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:text="学号:"
            android:id="@+id/tv_"
            />
        <TextView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:id="@+id/tv_id2"
            />

    </LinearLayout>


    <Button
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="获取"
     android:id="@+id/getname"
     />
</LinearLayout>

四.如何解析 JSONArray

使用getJSONObject(index);方法把数组下标为index的 JSONObject提取出来,存入一个新的 JSONObject中。

JSONObject jsonObject1=jsonArray.getJSONObject(0);//

这里写图片描述

主函数代码:

package com.example.a22120.day6_json;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private Button btn_get;
    private TextView tv_name, tv_age, tv_class, tv_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindID();
        btn_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* parseJson();
                parseJsonobj()*/
                ;
                parseJsonArray();
            }
        });

    }


    private void bindID() {
        btn_get = findViewById(R.id.getname);
        tv_age = findViewById(R.id.tv_age2);
        tv_name = findViewById(R.id.tv_name2);
        btn_get = findViewById(R.id.getname);
        tv_class = findViewById(R.id.tv_class2);
        tv_id = findViewById(R.id.tv_id2);
    }

    //解析jsonObj对象的方法
    private void parseJsonobj() {
    /*    {"name":"张三","age":21,"info":{"class":"三年一班","id":2016001}}*/
        String str = "{\"name\":\"张三\",\"age\":21,\"info\":{\"class\":\"三年一班\",\"id\":2016001}}";
        try {
            JSONObject jsonObject2 = new JSONObject(str);
            JSONObject classjson = jsonObject2.getJSONObject("info");
            tv_class.setText(classjson.getString("class"));
            tv_id.setText(classjson.getInt("id") + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    //ctrl+r 是替换的快捷键
    //解析JSON的方法
    private void parseJson() {
        String json_srt = " {\"name\":\"张三\",\"age\":21}";    //“\”为转义符
        try {
            JSONObject jsonObject = new JSONObject(json_srt);
            String name = jsonObject.getString("name");
            int age = jsonObject.getInt("age");
            tv_name.setText(name);
            tv_age.setText(Integer.toString(age)); //或者直接 用  age+""

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

    //解析JsonArray数组
    private void parseJsonArray() {
       /* [ {"name":"张三","age":21}, {"name":"李四","age":22}]*/
        String str = "[ {\"name\":\"张三\",\"age\":21}, {\"name\":\"李四\",\"age\":22}]";
        try {
            JSONArray jsonArray = new JSONArray(str);
            JSONObject jsonObject1 = jsonArray.getJSONObject(0);     //数组下标代表不同的json对象
            JSONObject jsonObject2 = jsonArray.getJSONObject(1);
            tv_name.setText(jsonObject1.getString("name"));
            tv_age.setText(jsonObject1.getString("age"));
            tv_class.setText(jsonObject2.getString("name"));
            tv_id.setText(jsonObject2.getString("age"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

UI代码与开始的一致

五.解析案例

将String str=”[ {\”name\”:\”张三\”,\”age\”:21,\”info\”:{\”class\”:\”三年一班\”,\”id\”:2016001}}, {\”name\”:\”李四\”,\”age\”:22,\”info\”:{\”
class\”:\”三年二班\”,\”id\”:2016002}}]”;String str=”[ {\”name\”:\”张三\”,\”age\”:21,\”info\”:{\”class\”:\”三年一班\”,\”id\”:2016001}}, {\”name\”:\”李四\”,\”age\”:22,\”info\”:{\”class\”:\”三年二班\”,\”id\”:2016002}}]”;

猜你喜欢

转载自blog.csdn.net/qq_38845493/article/details/80672912