Fastjson JSON与JSONObject转换

1 简单JSON与JSONObject的转换

1.1 简单JSON转为JSONObject

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONObject;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]){
        //简单JSON->JSONObject对象
        String  JSON_OBJ_STR = "{\"studentName\":\"eric\",\"studentAge\":12}";

        JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);

        System.out.println("studentName:  " + jsonObject.getString("studentName") + ":" + "  studentAge:  "
                + jsonObject.getInteger("studentAge"));

    }

}

运行效果为:

1.2 JSONObject转为简单JSON

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONObject;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]){

        String  JSON_OBJ_STR = "{\"studentName\":\"eric\",\"studentAge\":12}";

        //简单JSON->JSONObject对象
        JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);

        //JSONObject->简单JSON
        String jsonString = JSONObject.toJSONString(jsonObject);

        System.out.println(jsonString);

    }

}

运行效果为:

2 JSON数组与JSONArray的转换

2.1 JSON数组转为JSONArray

MainApp:

package com.yiidian.fastjson;

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

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]){
        //JSON数组
        String  JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";

        //JSON数组转换为JSONArray
        JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);

        //遍历方式1
        int size = jsonArray.size();
        for (int i = 0; i < size; i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);
            System.out.println("studentName:  " + jsonObject.getString("studentName") + ":" + "  studentAge:  "
                    + jsonObject.getInteger("studentAge"));
        }

        System.out.println("======================================");

        //遍历方式2
        for (Object obj : jsonArray) {

            JSONObject jsonObject = (JSONObject) obj;
            System.out.println("studentName:  " + jsonObject.getString("studentName") + ":" + "  studentAge:  "
                    + jsonObject.getInteger("studentAge"));
        }
    }

}

运行效果为:

2.2 JSONArray转为JSON数组

MainApp:

package com.yiidian.fastjson;

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

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]){
        //JSON数组
        String  JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
        
        JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);

        //JSONArray转换为JSON数组
        String jsonString = JSONArray.toJSONString(jsonArray);

        System.out.println(jsonString);
    }

}

运行效果为:

3 复杂JSON与JSONObject的转换

3.1 复杂JSON转为JSONObject

MainApp:

package com.yiidian.fastjson;

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

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]){
        //复杂JSON
        String  COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";

        //复杂JSON格式转为JSONObject
        JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);

        String teacherName = jsonObject.getString("teacherName");
        Integer teacherAge = jsonObject.getInteger("teacherAge");

        System.out.println("teacherName:  " + teacherName + "   teacherAge:  " + teacherAge);

        JSONObject jsonObjectcourse = jsonObject.getJSONObject("course");
        //获取JSONObject中的数据
        String courseName = jsonObjectcourse.getString("courseName");
        Integer code = jsonObjectcourse.getInteger("code");

        System.out.println("courseName:  " + courseName + "   code:  " + code);

        JSONArray jsonArraystudents = jsonObject.getJSONArray("students");

        //遍历JSONArray
        for (Object object : jsonArraystudents) {

            JSONObject jsonObjectone = (JSONObject) object;
            String studentName = jsonObjectone.getString("studentName");
            Integer studentAge = jsonObjectone.getInteger("studentAge");

            System.out.println("studentName:  " + studentName + "   studentAge:  " + studentAge);
        }
    }

}

运行效果为:

3.2 JSONObject转为复杂JSON

MainApp:

package com.yiidian.fastjson;

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

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]){
        //复杂JSON
        String  COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
        
        JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
        
        //JSONObject转为复杂JSON
        String jsonString = jsonObject.toJSONString();
        System.out.println(jsonString);
    }

}

运行效果为:

{"teacherAge":27,"teacherName":"crystall","course":{"courseName":"english","code":1270},"students":[{"studentAge":12,"studentName":"lily"},{"studentAge":15,"studentName":"lucy"}]}

 

热门文章

优秀文章