java to json / json to java

目录

1、准备 

2、java to  json 

3、json to java


1、准备 

1、json 格式在线查看

2.下载  阿里巴巴 json  解析库

alibaba/fastjson

下载最新的 jar 包 并且放在 项目 libs 目录下,add as lib````

2、java to  json 

比如我们想使用 java  编写以下 json 格式 数据

{
    "creatTime": 1992,
    "school": "一中",
    "class": [{
        "classOne": {
            "studentDetails": [{
                "high": 172.3,
                "name": "wang",
                "age": 18
            }, {
                "high": 165.8,
                "name": "li",
                "age": 20
            }],
            "teachers": {
                "chinese": "mrlu",
                "english": "msyang"
            },
            "studentsNum": 20,
            "master": "mrlu"
        }
    }, {
        "classTwo": {
            "studentDetails": [{
                "high": 160.8,
                "name": "liu",
                "age": 20
            }, {
                "high": 158.4,
                "name": "zhao",
                "age": 17
            }],
            "teachers": {
                "chinese": "mrliu",
                "english": "mszhang"
            },
            "studentsNum": 30,
            "master": "mszhang"
        }
    }]
}

代码实现:

package com.example.fortest;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MainActivity extends AppCompatActivity {

    private String TAG = "jsonTest";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        schoolJsonOnject();
    }


    //创建学生 json 对象
    private JSONObject studentJsonObject(String name,int age, float high) {
        JSONObject jsonObjectStudent  = new JSONObject();
        jsonObjectStudent.put("name",name);
        jsonObjectStudent.put("age",age);
        jsonObjectStudent.put("high",high);
        return jsonObjectStudent;
    }

    //创建老师 json 对象
    private JSONObject teacherJsonObject(String chinese ,String english) {
        JSONObject jsonObjectTeacher  = new JSONObject();
        jsonObjectTeacher.put("chinese",chinese);
        jsonObjectTeacher.put("english",english);
        return jsonObjectTeacher;
    }

    //创建班级 json 对象
    private JSONObject classJsonObject(String master , int studentsNum,
                                       JSONObject teacherJsonObject, JSONArray studentJsonArrayObject) {
        JSONObject jsonObjectClass  = new JSONObject();
        jsonObjectClass.put("master",master);
        jsonObjectClass.put("studentsNum",studentsNum);
        jsonObjectClass.put("teachers",teacherJsonObject);
        jsonObjectClass.put("studentDetails",studentJsonArrayObject);

        return jsonObjectClass;
    }

    //创建 第一个班 json 实体
    private JSONObject creatJsonObkectClassOne() {

        //创建 第一个班的学生
        JSONObject studentWang = studentJsonObject("wang",18,172.3f);
        JSONObject studentLi = studentJsonObject("li",20,165.8f);
        JSONArray jsonArrayStudent = new JSONArray();
        jsonArrayStudent.add(studentWang);
        jsonArrayStudent.add(studentLi);

        //创建 第一个 班的老师
        JSONObject jsonObjectTeacher = teacherJsonObject("mrlu","msyang");

        //创建 第一个班级
        JSONObject jsonObjectClass =
                classJsonObject("mrlu",20,jsonObjectTeacher,jsonArrayStudent);

        JSONObject jsonObjectClassOne = new JSONObject();

        jsonObjectClassOne.put("classOne",jsonObjectClass);

        return jsonObjectClassOne;
    }

    //创建 第二个班 json 实体
    private JSONObject creatJsonObkectClassTwo() {

        //创建 第个班的学生
        JSONObject studentLiu = studentJsonObject("liu",20,160.8f);
        JSONObject studentZhao = studentJsonObject("zhao",17,158.4f);
        JSONArray jsonArrayStudent = new JSONArray();
        jsonArrayStudent.add(studentLiu);
        jsonArrayStudent.add(studentZhao);

        //创建 第个 班的老师
        JSONObject jsonObjectTeacher = teacherJsonObject("mrliu","mszhang");

        //创建 第个班级
        JSONObject jsonObjectClass =
                classJsonObject("mrliu",30,jsonObjectTeacher,jsonArrayStudent);

        JSONObject jsonObjectClassTwo = new JSONObject();

        jsonObjectClassTwo.put("classTwos",jsonObjectClass);

        return jsonObjectClassTwo;
    }


    private JSONObject schoolJsonOnject() {
        JSONObject jsonObjectSchool  = new JSONObject();
        jsonObjectSchool.put("school","一中");
        jsonObjectSchool.put("creatTime",1992);

        JSONArray jsonArrayClasses = new JSONArray();

        JSONObject classOne = creatJsonObkectClassOne();
        jsonArrayClasses.add(classOne);

        JSONObject classTwo = creatJsonObkectClassTwo();
        jsonArrayClasses.add(classTwo);

        jsonObjectSchool.put("class",jsonArrayClasses);


        Log.i(TAG,jsonObjectSchool.toJSONString());
        return jsonObjectSchool;
    }
}

然后我们 把打印的 log 放在json在线格式中,发现就是我们准备转化的数据。

3、json to java

那我们怎么通过 json 格式的数据 获取到我们的  java 数据呢。

我们依获取 classone wang 同学的 age 为例:

package com.example.fortest;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MainActivity extends AppCompatActivity {

    private String TAG = "jsonTest";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        schoolJsonOnject();
    }


    //创建学生 json 对象
    private JSONObject studentJsonObject(String name,int age, float high) {
        JSONObject jsonObjectStudent  = new JSONObject();
        jsonObjectStudent.put("name",name);
        jsonObjectStudent.put("age",age);
        jsonObjectStudent.put("high",high);
        return jsonObjectStudent;
    }

    //创建老师 json 对象
    private JSONObject teacherJsonObject(String chinese ,String english) {
        JSONObject jsonObjectTeacher  = new JSONObject();
        jsonObjectTeacher.put("chinese",chinese);
        jsonObjectTeacher.put("english",english);
        return jsonObjectTeacher;
    }

    //创建班级 json 对象
    private JSONObject classJsonObject(String master , int studentsNum,
                                       JSONObject teacherJsonObject, JSONArray studentJsonArrayObject) {
        JSONObject jsonObjectClass  = new JSONObject();
        jsonObjectClass.put("master",master);
        jsonObjectClass.put("studentsNum",studentsNum);
        jsonObjectClass.put("teachers",teacherJsonObject);
        jsonObjectClass.put("studentDetails",studentJsonArrayObject);

        return jsonObjectClass;
    }

    //创建 第一个班 json 实体
    private JSONObject creatJsonObkectClassOne() {

        //创建 第一个班的学生
        JSONObject studentWang = studentJsonObject("wang",18,172.3f);
        JSONObject studentLi = studentJsonObject("li",20,165.8f);
        JSONArray jsonArrayStudent = new JSONArray();
        jsonArrayStudent.add(studentWang);
        jsonArrayStudent.add(studentLi);

        //创建 第一个 班的老师
        JSONObject jsonObjectTeacher = teacherJsonObject("mrlu","msyang");

        //创建 第一个班级
        JSONObject jsonObjectClass =
                classJsonObject("mrlu",20,jsonObjectTeacher,jsonArrayStudent);

        JSONObject jsonObjectClassOne = new JSONObject();

        jsonObjectClassOne.put("classOne",jsonObjectClass);

        return jsonObjectClassOne;
    }

    //创建 第二个班 json 实体
    private JSONObject creatJsonObkectClassTwo() {

        //创建 第个班的学生
        JSONObject studentLiu = studentJsonObject("liu",20,160.8f);
        JSONObject studentZhao = studentJsonObject("zhao",17,158.4f);
        JSONArray jsonArrayStudent = new JSONArray();
        jsonArrayStudent.add(studentLiu);
        jsonArrayStudent.add(studentZhao);

        //创建 第个 班的老师
        JSONObject jsonObjectTeacher = teacherJsonObject("mrliu","mszhang");

        //创建 第个班级
        JSONObject jsonObjectClass =
                classJsonObject("mrliu",30,jsonObjectTeacher,jsonArrayStudent);

        JSONObject jsonObjectClassTwo = new JSONObject();

        jsonObjectClassTwo.put("classTwos",jsonObjectClass);

        return jsonObjectClassTwo;
    }


    private JSONObject schoolJsonOnject() {
        JSONObject jsonObjectSchool  = new JSONObject();
        jsonObjectSchool.put("school","一中");
        jsonObjectSchool.put("creatTime",1992);

        JSONArray jsonArrayClasses = new JSONArray();

        JSONObject classOne = creatJsonObkectClassOne();
        jsonArrayClasses.add(classOne);

        JSONObject classTwo = creatJsonObkectClassTwo();
        jsonArrayClasses.add(classTwo);

        jsonObjectSchool.put("class",jsonArrayClasses);


        Log.i(TAG,jsonObjectSchool.toJSONString());

        jsonTojava(jsonObjectSchool.toJSONString());

        return jsonObjectSchool;
    }


    private void jsonTojava(String jsonString ) {
        JSONObject jsonObjectSchool = JSON.parseObject(jsonString);
        String  school = jsonObjectSchool.getString("school");

        JSONArray jsonArrayClass = jsonObjectSchool.getJSONArray("class");
        for (Object classOnject: jsonArrayClass) {
            JSONObject jsonObject = (JSONObject) classOnject;
            if (jsonObject.containsKey("classOne")) {
                JSONObject jsonObjectOne = (JSONObject) jsonObject.get("classOne");
                JSONArray studentJsonArry = jsonObjectOne.getJSONArray("studentDetails");
                for (Object student : studentJsonArry) {
                    JSONObject studentObject = (JSONObject) student;
                    if (studentObject.getString("name").equals("wang")) {
                        int age = studentObject.getInteger("age");

                        Log.i(TAG,"school:  " + school +  "  wang age " + age);
                    }
                }
            }
        }
    }
}

log 显示 如下:

文件参考:

使用FastJson对JSON字符串、JSON对象及JavaBean之间的相互转换​​​​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值