json,jsonObject , jsonArray 详解

无论是前端还是后端, java开发中经常使用到 json 格式数据

1,json 的语法如下图:

这里写图片描述

2,java 后端中操作 json格式的类为 net.sf.json.JSONObject,常用的 api 有:

public static void main(String[] args) {
        // 创建一个JSONObject对象
        JSONObject jsonObject = new JSONObject();

        // 将一个对象转换为JSONObject对象
        Map<String, String> map = new HashMap<String, String>();
        map.put("we", "dsds");
        JSONObject jsonObject2 = JSONObject
                .fromObject(map);
        JSONObject jsonObject3 = JSONObject.fromObject(new Person());
        JSONObject jsonObject4 = JSONObject
                .fromObject("{obj: [{name: 'wwp'}, {age: 22}], obj: 2}");

        //将一个JSONObject对象转换为其它类型
        Map<String, String> map2 = (Map)JSONObject.toBean(jsonObject2, Map.class);
        Person person = (Person)JSONObject.toBean(jsonObject3, Person.class);

        //获取JSONObject对象 键的集合
        for(Object o: jsonObject4.keySet()){
            System.out.println(jsonObject4.get("obj"));
        }

        //put() 和 get() 方法
        jsonObject2.put("key", "sds");
        jsonObject2.get("key"); //注意:如果键不存在,则抛出异常 

    }

3,json 和 xml 之间的相互转换

转换 xml 格式数据为 json 格式

package jsontest;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

public class jsonTest {
    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password>123456</password><username>张三</username></user_info>";
        XMLSerializer xSerializer = new XMLSerializer();
        JSON json = xSerializer.read(xml);
        JSONObject jsonObject = JSONObject.fromObject(json);
        System.out.println(jsonObject.get("password33"));
    }
}

json 格式数据转换为 xml 格式:

package jsontest;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

public class jsonTest {
    public static void main(String[] args) {
        XMLSerializer xSerializer = new XMLSerializer();
        JSONObject jsonObject = JSONObject.fromObject("{name: 'wwp', password: 'sddw'}");
        xSerializer.setRootName("userInfo");
        xSerializer.setTypeHintsEnabled(false);
        String json = xSerializer.write(jsonObject);
        System.out.println(json);
    }
}

4,json 与 java 对象之间的相互转换

将java 对象中的 日期对象转换为 特定字符串格式方法:

JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
            private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            @Override
            public Object processObjectValue(String arg0, Object date, JsonConfig arg2) {

                return sdf.format(date);
            }

            @Override
            public Object processArrayValue(Object date, JsonConfig arg1) {

                return sdf.format(date);
            }
        });

        JSONObject json = JSONObject.fromObject(school, jsonConfig);

json –> bean ,如果bean中有其它类的集合,可以使用如下方法解决:

Map<String, Object> mapClass = new HashMap<String, Object>();
        mapClass.put("classRooms", ClassRoom.class);
        mapClass.put("students", Student.class);

        School sch = (School) JSONObject.toBean(json, School.class, mapClass);
        System.out.println(sch.getClassRooms().get(0).getStudents().get(0).getBirthday());
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值