细细品味Web——Json-Lib类库使用手册

Java JSON-lib 库:从基础到高级,一步步深入 简单来说,JSON-lib是一个用于Java中JSON数据解析的库。好了,今天我们从零开始,一步步深入了解了JSON-lib的使用方法。从简单的对象序列化,到自定义序列化规则,再到处理复杂类型,我们不仅学到了如何使用JSON-lib,更重要的是学会了如何调试和解决实际问题。比如,如果你的对象结构太复杂,可能会出现循环引用的问题。但是如果我们的对象里包含了一些复杂的类型,比如日期或者自定义的类怎么办呢?来格式化日期,这样输出的JSON字符串中日期就会按照我们指定的格式显示了。别急,让我们继续深入。 阅读详情
Json-lib包是一个Java类库,提供将 Java对象(包括:beans,maps,collections,java arrays 和 XML等)和 JSON互相转换的功能。

    相关的Jar包:

 

  • json-lib-2.3-jdk15.jar
  • commons-beanutils-1.8.0.jar
  • commons-collections-3.1.jar
  • commons-lang-2.4.jar
  • commons-logging-1.1.1.jar
  • ezmorph-1.0.6.jar

 

    备注:我们这里用的Struts-2.3.3版本中Json-lib包。

    官方网站:http://json-lib.sourceforge.net/

 

    下表中是Java与JavaScript类型对应关系。

 

JSON

 

Java

string

<=>

java.lang.String, java.lang.Character, char

number

<=>

java.lang.Number, byte, short, int, long, float, double

true|false 

<=> 

java.lang.Boolean, boolean 

null

<=>

null

function 

<=>

net.sf.json.JSONFunction 

array 

<=>

net.sf.json.JSONArray (object, string, number, boolean, function) 

object 

<=>

net.sf.json.JSONObject 

 

2、JsonLib使用

2.1 准备工作

    我们这里使用JUnit4.0进行测试实验,下面是测试中使用中的类。

    【测试实体——Student】

 

package com.hebut.jsonlib;

 

public class Student {

    private String userName;

    private String sex;

    private int age;

   

    public Student() {

    }

   

    public Student(String userName, String sex, int age) {

        this.userName = userName;

        this.sex = sex;

        this.age = age;

    }

 

    public String getUserName() {

        return userName;

    }

    public void setUserName(String userName) {

        this.userName = userName;

    }

    public String getSex() {

        return sex;

    }

   

    public void setSex(String sex) {

        this.sex = sex;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

}

 

    【测试实体——Class】

 

package com.hebut.jsonlib;

 

import java.util.Date;

import java.util.List;

 

public class Class {

    private String name;

    private Date date;

    private List<Student> students;

   

    public Class() {

    }

   

    public Class(String name, Date date, List<Student> students) {

        this.name = name;

        this.date = date;

        this.students = students;

    }

   

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public Date getDate() {

        return date;

    }

    public void setDate(Date date) {

        this.date = date;

    }

    public List<Student> getStudents() {

        return students;

    }

    public void setStudents(List<Student> students) {

        this.students = students;

    }

}

 

2.2 处理ArraysToJson

    【示例代码】

 

package com.hebut.jsonlib;

 

import java.util.ArrayList;

import java.util.List;

import net.sf.json.JSONArray;

import org.junit.Test;

 

public class JsonLib {

 

    @Test

    public void array2json() {

        int[] intArray = new int[]{1,4,5};

        JSONArray jsonArray1 = JSONArray.fromObject(intArray);

        System.out.println("int[] intArray");

        System.out.println(jsonArray1);

       

        boolean[] boolArray = new boolean[]{true,false,true};

        System.out.println("boolean[] boolArray");

        JSONArray jsonArray2 = JSONArray.fromObject(boolArray);

        System.out.println(jsonArray2);

       

        int[][] int2Array = new int[][]{{1,2},{3,4}};

        JSONArray jsonArray3 = JSONArray.fromObject(int2Array);

        System.out.println("int[][] int2Array");

        System.out.println(jsonArray3);

       

        float[] floatArray = new float[]{0.1f,0.2f,0.3f};

        JSONArray jsonArray4 = JSONArray.fromObject(floatArray);

        System.out.println("float[] floatArray");

        System.out.println(jsonArray4);

       

        String[] strArray = new String[]{"hello","hebut","xiapi"};

        JSONArray jsonArray5 = JSONArray.fromObject(strArray);

        System.out.println("String[] strArray");

        System.out.println(jsonArray5);

    }

}

 

    【运行结果】

 

int[] intArray

[1,4,5]

boolean[] boolArray

[true,false,true]

int[][] int2Array

[[1,2],[3,4]]

float[] floatArray

[0.1,0.2,0.3]

String[] strArray

["hello","hebut","xiapi"]

 

2.3 处理CollectionsToJson

    【示例代码】

 

package com.hebut.jsonlib;

 

import java.util.ArrayList;

import java.util.List;

import net.sf.json.JSONArray;

import org.junit.Test;

 

public class JsonLib {

   

    @Test

    public void collections2json(){

        List list1 = new ArrayList();

        list1.add("first");

        list1.add("second");

        JSONArray jsonArray1 = JSONArray.fromObject(list1);

        System.out.println("List list1");

        System.out.println(jsonArray1);

       

        List<Student> list2 = new ArrayList<Student>();

        list2.add(new Student("xiapi1","男",10));

        list2.add(new Student("xiapi2","女",11));

        list2.add(new Student("xiapi3","男",12));

        JSONArray jsonArray2 = JSONArray.fromObject(list2);

        System.out.println("List<Student> list2");

        System.out.println(jsonArray2);

    }

}

 

    【运行结果】

 

List list1

["first","second"]

List<Student> list2

[{"age":10,"sex":"男","userName":"xiapi1"},{"age":11,"sex":"女","userName":"xiapi2"},{"age":12,"sex":"男","userName":"xiapi3"}]

 

2.4 处理MapToJson

    【示例代码】

 

package com.hebut.jsonlib;

 

import java.util.HashMap;

import java.util.Map;

import net.sf.json.JSONObject;

import org.junit.Test;

 

public class JsonLib {

   

    @Test

    public void map2json(){

        Map map1 = new HashMap();

        map1.put("name","json");

        map1.put("bool",Boolean.TRUE);

        map1.put("int",new Integer(1));

        map1.put("arr",new String[]{"a","b"});

        map1.put("func","function(i){ return this.arr[i]; }");

        JSONObject jsonObject1 = JSONObject.fromObject(map1);

        System.out.println("Map map1");

        System.out.println(jsonObject1);

       

        Map<String,Student> map2 = new HashMap<String,Student>();

        map2.put("k1", new Student("xiapi1","男",10));

        map2.put("k2", new Student("xiapi2","女",12));

        map2.put("k3", new Student("xiapi3","男",13));

        JSONObject jsonObject2 = JSONObject.fromObject(map2);

        System.out.println("Map<String,Student> map2");

        System.out.println(jsonObject2);

    }

}

 

    【运行结果】

 

Map map1

{"arr":["a","b"],"int":1,"name":"json","func":function(i){ return this.arr[i]; },"bool":true}

Map<String,Student> map2

{"k3":{"age":13,"sex":"男","userName":"xiapi3"},"k1":{"age":10,"sex":"男","userName":"xiapi1"},"k2":{"age":12,"sex":"女","userName":"xiapi2"}}

 

2.5 处理BeanToJson

    【示例代码】

 

package com.hebut.jsonlib;

 

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

 

import net.sf.json.JSON;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import net.sf.json.JsonConfig;

import net.sf.json.processors.JsonValueProcessor;

import net.sf.json.util.CycleDetectionStrategy;

import org.junit.Test;

 

public class JsonLib {

   

    @Test

    public void bean2json(){

        Student s1 = new Student("xiapi","男",22);

        JSONObject jsonObject1 = JSONObject.fromObject(s1);

        System.out.println("Student s1");

        System.out.println(jsonObject1);

       

        Class c1 = new Class();

        c1.setName("计算机应用1班");

        c1.setDate(new Date());

        JsonConfig config=new JsonConfig();

        //设置循环策略为忽略

        config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

        //设置 json转换的处理器用来处理日期类型

        //凡是反序列化Date类型的对象,都会经过该处理器进行处理

        config.registerJsonValueProcessor(Date.class,

                                    new JsonValueProcessor() {

            //参数1 :属性名参数2:json对象的值参数3:jsonConfig对象

            public Object processObjectValue(String arg0, Object arg1,JsonConfig arg2) {

                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                Date d=(Date) arg1;

                return sdf.format(d);

            }

            public Object processArrayValue(Object arg0, JsonConfig arg1) {

                return null;

            }

        });

        List<Student> students = new ArrayList<Student>();

        students.add(new Student("xiapi1","男",10));

        students.add(new Student("xiapi2","女",11));

        students.add(new Student("xiapi3","男",12));

        c1.setStudents(students);

        JSONObject jsonObject2 = JSONObject.fromObject(c1,config);

        System.out.println("Class c1");

        System.out.println(jsonObject2);

    }

}

 

    【运行结果】

 

Student s1

{"age":22,"sex":"男","userName":"xiapi"}

Class c1

{"date":"2012-05-21 19:19:31","name":"计算机应用1班","students":[{"age":10,"sex":"男","userName":"xiapi1"},{"age":11,"sex":"女","userName":"xiapi2"},{"age":12,"sex":"男","userName":"xiapi3"}]}

 

2.6 处理XmlToJson

    在处理XML时,需要另添加"xom-1.2.8.jar"包。

    下载地址:http://www.xom.nu/

    【示例代码】

 

package com.hebut.jsonlib;

 

import net.sf.json.JSON;

import net.sf.json.xml.XMLSerializer;

import org.junit.Test;

 

public class JsonLib {

   

    @Test

    public void xml2json(){

        String s="<student>

                        <name id='n1'>xiapi</name>

                        <sex class='s1'>男</sex>

                        <age>20</age>

                    </student>";

        XMLSerializer x =new XMLSerializer();

        JSON json = x.read(s);

        System.out.println("XmlToJson");

        System.out.println(json.toString());

    }

}

 

    【运行结果】

 

2012-5-21 19:01:03 net.sf.json.xml.XMLSerializer getType

信息: Using default type string

XmlToJson

{"name":{"@id":"n1","#text":"xiapi"},"sex":{"@id":"s1","#text":"男"},"age":"20"}

 

2.7 处理JsonToArrays

    【示例代码】

 

package com.hebut.jsonlib;

 

import net.sf.ezmorph.test.ArrayAssertions;

import net.sf.json.JSONArray;

import net.sf.json.JSONSerializer;

import net.sf.json.JsonConfig;

 

import org.junit.Test;

 

public class JsonLib {

   

    @Test

    public void json2arrays(){

        String json1 = "['first','second']";

        JSONArray jsonArray1 = (JSONArray) JSONSerializer.toJSON(json1);

        JsonConfig jsonConfig = new JsonConfig();

        jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY);

        Object[] output1 = (Object[]) JSONSerializer.toJava(jsonArray1, jsonConfig);

        Object[] expected = new Object[] { "first", "second" };

        ArrayAssertions.assertEquals(expected, output1);

        System.out.println("Object[]");

        System.out.println(output1.length);

        System.out.println(output1[1]);

       

        String json2 ="[[1,2],[3,4]]";

        JSONArray jsonArray2 = JSONArray.fromObject(json2);

        Object[][] output2 = (Object[][])JSONArray.toArray(jsonArray2);

        System.out.println("Object[][]");

        System.out.println(output2.length);

        System.out.println(output2[0][0]);

    }

}

 

    【运行结果】

 

Object[]

2

second

Object[][]

2

1

 

2.8 处理JsonToCollections

    【示例代码】

 

package com.hebut.jsonlib;

 

import java.util.List;

import net.sf.json.JSONArray;

import net.sf.json.JSONSerializer;

import org.junit.Test;

 

public class JsonLib {

   

    @Test

    public void json2collections(){

        String json1 = "['first','second']";

        JSONArray jsonArray1 = (JSONArray) JSONSerializer.toJSON(json1);

        List output1 = (List) JSONSerializer.toJava(jsonArray1);

        System.out.println("List");

        System.out.println(output1.get(0));

       

        String json2 = "[{'age':10,'sex':'男','userName':'xiapi1'},

                        {'age':11,'sex':'女','userName':'xiapi2'}]";

        JSONArray jsonArray2 = JSONArray.fromObject(json2);

        List<Student> output2 = JSONArray.toList(jsonArray2,Student.class);

        System.out.println("List<Student>");

        System.out.println(output2.size());

        System.out.println(output2.get(0));

        System.out.println(output2.get(0).getUserName());

    }

}

 

    【运行结果】

 

List

first

List<Student>

2

com.hebut.jsonlib.Student@16f144c

xiapi1

 

2.9 处理JsonToMap

    【示例代码】

 

package com.hebut.jsonlib;

 

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import net.sf.ezmorph.Morpher;

import net.sf.ezmorph.MorpherRegistry;

import net.sf.ezmorph.bean.BeanMorpher;

import net.sf.ezmorph.test.ArrayAssertions;

import net.sf.json.JSONObject;

import net.sf.json.JSONSerializer;

import net.sf.json.util.JSONUtils;

import net.sf.json.xml.XMLSerializer;

 

import org.junit.Test;

 

public class JsonLib {

 

    @Test

    public void json2map(){

        String json1 ="{'arr':['a','b'],'int':1,

                        'name':'json','bool':true}";

        JSONObject jsonObject1 = JSONObject.fromObject(json1);

        Map typeMap1 = new HashMap();

        typeMap1.put("arr", String[].class);

        typeMap1.put("int", Integer.class);

        typeMap1.put("name", String.class);

        typeMap1.put("bool", Boolean.class);

        Map output1 = (Map)JSONObject.toBean(jsonObject1, Map.class,typeMap1);

        System.out.println("Map");

        System.out.println(output1.size());

        System.out.println(output1.get("name"));

        System.out.println(output1.get("arr"));

       

        String json2 ="{'k1':{'age':10,'sex':'男','userName':'xiapi1'},

                        'k2':{'age':12,'sex':'女','userName':'xiapi2'}}";

        JSONObject jsonObject2 = JSONObject.fromObject(json2);

        Map<String,Class<?>> typeMap2 =new HashMap<String,Class<?>>();

        Map<String,Student> output2 = (Map<String,Student>)JSONObject.toBean(jsonObject2,Map.class,typeMap2);

        System.out.println("Map<String,Student>");

        System.out.println(output2.size());

        System.out.println(output2.get("k1"));

       

        //先往注册器中注册变换器,需要用到ezmorph包中的类

    MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();

    Morpher dynaMorpher = new BeanMorpher(Student.class,morpherRegistry);

    morpherRegistry.registerMorpher(dynaMorpher);

 

        System.out.println(((Student)morpherRegistry.morph(Student.class,output2.get("k2"))).getUserName());

    }

}

 

    【运行结果】

 

Map

4

json

[a, b]

Map<String,Student>

2

net.sf.ezmorph.bean.MorphDynaBean@5b8827[

{sex=男, age=10, userName=xiapi1}

]

xiapi2

 

2.10 处理JsonToBean

    【示例代码】

 

package com.hebut.jsonlib;

 

import net.sf.ezmorph.Morpher;

import net.sf.ezmorph.MorpherRegistry;

import net.sf.ezmorph.bean.BeanMorpher;

import net.sf.ezmorph.object.DateMorpher;

import net.sf.json.JSONObject;

import net.sf.json.JSONSerializer;

import net.sf.json.util.JSONUtils;

 

import org.junit.Test;

 

public class JsonLib {

   

    @Test

    public void json2bean(){

        //简单对象

        String json1 = "{'age':22,'sex':'男','userName':'xiapi'}";

        JSONObject jsonObject1 = JSONObject.fromObject(json1);

        Student output1 = (Student)JSONObject.toBean(jsonObject1,Student.class);

        System.out.println("Student");

        System.out.println(output1.getUserName());

       

        //复杂对象

        String json2 = "{'date':'2012-05-21 13:03:11',

                            'name':'计算机应用1班',

                'students':[{'age':10,'sex':'男','userName':'xiapi1'},

                            {'age':11,'sex':'女','userName':'xiapi2'}]}";

        //转为日期

        String[] DATE_FORMAT = { "yyyy-MM-dd HH:mm:ss" };

        MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();

        morpherRegistry.registerMorpher(new DateMorpher(DATE_FORMAT));

        JSONObject jsonObject2 = JSONObject.fromObject(json2);

        Map typeMap1 = new HashMap();

        typeMap1.put("date", Date.class);

        typeMap1.put("name",String.class);

        typeMap1.put("students", Student.class);

        Class output2 = (Class)JSONObject.toBean(jsonObject2,Class.class,typeMap1);

        System.out.println("Class");

        System.out.println(output2.getName());

        System.out.println(output2.getDate());

        System.out.println(output2.getStudents().get(0).getUserName());

    }

}

 

    【运行结果】

 

Student

xiapi

Class

计算机应用1班

Mon May 21 13:03:11 CST 2012

xiapi1

 

2.11 处理JsonToXml

    【示例代码】

 

package com.hebut.jsonlib;

 

import net.sf.json.JSONObject;

import net.sf.json.xml.XMLSerializer;

import org.junit.Test;

 

public class JsonLib {

   

    @Test

    public void json2xml(){

        String json1 = "{'age':22,'sex':'男','userName':'xiapi'}";

        JSONObject jsonObj = JSONObject.fromObject(json1);

    XMLSerializer x = new XMLSerializer();

    String xml = x.write(jsonObj);

    System.out.println("XML");

    System.out.println(xml);

    }

}

 

    【运行结果】

 

XML

<?xml version="1.0" encoding="UTF-8"?>

<o><age type="number">22</age><sex type="string">男</sex><userName type="string">xiapi</userName></o>

 

2.12 对象JSONArray

    【示例代码】

 

package com.hebut.jsonlib;

 

import net.sf.json.JSONArray;

import org.junit.Test;

 

public class JsonLib2 {

 

    @Test

    public void testJSONArray() {

        JSONArray jsonArray = new JSONArray();

        jsonArray.add(0,"第一个值");

        jsonArray.add(1,"第二个值");

        jsonArray.add(2,"第三个值");

        System.out.print(jsonArray.toString());

    }

}

 

    【运行结果】

 

["第一个值","第二个值","第三个值"]

 

2.13 对象JSONObject

    【示例代码】

 

package com.hebut.jsonlib;

 

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

 

import org.junit.Test;

 

public class JsonLib2 {

 

    @Test

    public void testJSONObject() {

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("name", "xiapi");

        jsonObject.put("age", 20);

        jsonObject.put("sex", "男");

        JSONArray jsonArray = new JSONArray();

        jsonArray.add("唱歌");

        jsonArray.add("摄影");

        jsonArray.add("象棋");

        jsonObject.element("hobby",jsonArray);

        System.out.println(jsonObject);

    }

}

 

    【运行结果】

 

{"name":"xiapi","age":20,"sex":"男","hobby":["唱歌","摄影","象棋"]}

 

3、日期转换

  json-lib转换出来的日期类型格式:

 

"birthday":{"date":1,"day":0,"hours":0,"minutes":0,"month":7,"nanos":0,"seconds":0,"time":1280592000000,"timezoneOffset":-480,"year":110}

 

  那么我们如果想要"yyyy-MM-dd HH:mm:ss "这种格式的怎么办呢?

3.1 三种解决方案

  都必须使用jsonConfig对象进行处理

  (1)使用jsonConfig的setExcludes的方法进行过滤,将所需要过滤的字段名传入setExcludes方法。

 

    public void objectToJson(){

        //创建对象

        Emp emp= new Emp(****);

        //创建jsonConfig对象

        JsonConfig config=new JsonConfig();

        //设置过滤字段

        config.setExcludes(new String[]{"dept"});

        String s=JSONObject.fromObject(emp,config).toString();

        System.out.println(s);

    }

 

  (2)使用jsonConfig的setJsonPropertyFilter进行属性过滤,过滤器中返回true表示过滤该字段,返回false表示可以转换该字段。

 

    public void objectToJson(){

        //创建对象

        Emp emp= new Emp(****);

        //创建jsonConfig对象

        JsonConfig config=new JsonConfig();

        //设置过滤字段

        config.setJsonPropertyFilter(new PropertyFilter() {

            public boolean apply(Object arg0, String arg1, Object arg2) {

                if("dept".equals(arg1)){

                    return true;

                }

                return false;

            }

        });

        String s=JSONObject.fromObject(emp,config).toString();

        System.out.println(s);

    }

 

  上述两种解决方案可以解决部分问题,但是json-lib使用代理进行反射,所以如果想要部门表的信息,而去过滤部门表的员工对象,这样还是解决不了。这样可以使用更简单有效的方案

 

  (3)使用jsonConfig的setCycleDetectionStrategy()方法进行忽略死循环。

         使用jsonConfig的registerJsonValueProcessor()进行属性转换设置。

 

    public void objectToJson(){

        //创建对象

        Emp emp= new Emp(****);

        //创建jsonConfig对象

        JsonConfig config=new JsonConfig();

        //设置循环策略为忽略 解决json最头疼的问题 死循环

        config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

        //设置 json转换的处理器 用来处理日期类型

        //凡是反序列化Date类型的对象,都会经过该处理器进行处理

        config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {

            //参数1 :属性名 参数2:json对象的值 参数3:jsonConfig对象

            public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {

                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                Date d=(Date) arg1;

                return sdf.format(d);

            }

            public Object processArrayValue(Object arg0, JsonConfig arg1) {

                return null;

            }

        });

        String s=JSONObject.fromObject(emp,config).toString();

        System.out.println(s);

    }

 

  这种方案可以解决死循环问题和日期格式化的问题。

  结果:

 

{"birthday":"2010-08-01 00:00:00","dept":{"depid":1,"depname":"开发部","emps":[{"birthday":"2009-08-01 00:00:00","dept":null,"empid":30,"empname":"田七"},null]},"empid":27,"empname":"王五"}

 

3.2 封装函数方案

 

public static String convert2Json2(Object object) {

    JsonConfig jsonConfig = new JsonConfig();

    jsonConfig.registerJsonValueProcessor(java.util.Date.class,

            new JsonValueProcessor() {

                private final String format = "yyyy-MM-dd hh:mm:ss";

 

                public Object processArrayValue(Object object,

                        JsonConfig jsonConfig) {

                    return null;

                }

 

                public Object processObjectValue(String string,

                        Object object, JsonConfig jsonConfig) {

                    if (null == object) {

                        return "";

                    } else {

                        if (object instanceof java.util.Date) {

                            SimpleDateFormat simpleDateFormat = new

                                SimpleDateFormat(format);

                            String dateStr = simpleDateFormat

                                    .format(object);

                            return dateStr;

                        }

                    }

 

                    return object.toString();

                }

            });

 

    if (object instanceof String)

        return object.toString();

    if ((object instanceof Object[]) || (object instanceof List)) {

        JSONArray jsonObject = JSONArray.fromObject(object, jsonConfig);

        return jsonObject.toString() + '\n';

    } else {

        JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig);

        return jsonObject.toString() + '\n';

    }

}

五十六、Java的json解析库Json-lib和Gson @Author:Runsen @Dater:2020/06/18 文章目录JSONJson-libGson JSON JSON不管是在Web开发还是服务器开发中是相当常见的数据传输格式,一般情况我们对于JSON解析构造的性能并不需要过于关心,除非是在性能要求比较高的系统。目前对于Java开源的JSON类库有很多种,下面我们取4个常用的JSON库进行性能测试对比, 同时根据测试结果分析如果根据实际应用场景选择最合适的JSON库。 首先写一个java类,需要导入一个包import org.json.*;这个包j 阅读详情

相关推荐

json-lib-2.4-jdk15:Java中的JSON处理工具

本文还有配套的精品资源,点击获取 简介:JSON-Lib是Java语言的一个库,用于实现Java对象和JSON格式数据之间的转换,简化了JSON数据处理。特别是json-lib-2.4-jdk15版本,专为JDK 1.5进行了优化,并支持泛型和枚举等特性。该库能够处理多种Java对象,例如集合、数组和日期等,并且能够将这些对象转换成JSON字符串,或者将JSON字符串解析回...

weixin_35752122的博客 1486

json-lib使用详解——json小工具

json-lib 一、json-lib是什么? apache提供的json-lib小工具,它可以方便的使用Java语言来创建JSON字符串。也可以把JavaBean转换成JSON字符串。 二、核心jar包 json-lib的核心jar包有: json-lib.jar json-lib的依赖jar包有: commons-lang.jar commons-beanutils.jar common...

刘伟佳的博客 7690

json-lib完整JAR包库集成指南

本文还有配套的精品资源,点击获取 简介:JSON是一种广泛用于Web应用的数据交换格式,json-lib是Java中一个流行的库,用于实现Java对象与JSON格式的相互转换。本压缩文件包含json-lib所需的所有JAR文件,以支持开发者在Java项目中轻松集成并使用该库。介绍了如何在Eclipse和IntelliJ IDEA等IDE中以及通过Maven/Gradle添加...

weixin_29069575的博客 1405

【转】细细品味Web——Json-Lib类库使用手册

1、JsonLib简介     Json-lib包 是一个Java类库,提供将Java对象 (包括:beans,maps,collections,java arrays 和 XML等)和JSON互相转换 的功能 。     相关的Jar包:   json-lib-2.3-jdk15.jar commons-beanutils-1....

nhy520 517

json-lib:Java JSON处理库的全面指南与jar包下载

本文还有配套的精品资源,点击获取 简介:JSON是一种广泛使用的轻量级数据交换格式,json-lib为Java开发人员提供了一套丰富的API来实现JSON数据与Java对象之间的转换。本指南深入讲解json-lib的使用方法、功能特性以及在实际开发中的应用实例,帮助开发者掌握如何序列化和反序列化数据,并探索json-lib支持的数据类型转换、XML与JSON互转以及JSON...

weixin_28988985的博客 1256

JSON数据处理库 - json-lib全面解析

JSON-lib是一个流行的Java库,专门用于处理JSON数据格式。它提供了将Java对象和JSON字符串之间进行互相转换的功能,并能处理常见的JSON数据结构。与Java原生的JSON处理能力相比,JSON-lib的特色在于对复杂JSON数据的处理能力,以及对JavaBean、数组和集合的原生支持。本章节我们将从JSON-lib的起源讲起,解释它的优势、功能,并概览其在实际开发中的应用场景。## 1.1 JSON-lib的背景与发展。

weixin_35755823的博客 1112

四种JSON类库的性能比较---Gson、FastJson、Jackson、Json-lib

本篇通过JMH来测试一下Java中几种常见的JSON解析库的性能。每次都在网上看到别人说什么某某库性能是如何如何的好,碾压其他的库。但是百闻不如一见,只有自己亲手测试过的才是最值得相信的。 JSON不管是在Web开发还是服务器开发中是相当常见的数据传输格式,一般情况我们对于JSON解析构造的性能并不需要过于关心,除非是在性能要求比较高的系统。 目前对于Java开源的JSON类库有很多种,下面我们取4个常用的JSON库进行性能测试对比, 同时根据测试结果分析如果根据实际应用场景选择最合适的JSON库。 这4个

qq_33449307的博客 1903

JSON库文件:使用lib和json.h简化数据交互

在软件开发中,库文件通常是一种预编译代码的集合,它允许开发者在自己的应用程序中使用这些代码,而无需重新编写。对于JSON库而言,它是一系列处理JSON数据结构的函数、类和方法的集合。使用JSON库文件,开发者可以轻松地进行JSON数据的解析、生成和操作,无需从头开始编写JSON处理的代码,极大地提高了开发效率并确保了实现的可靠性。Json::Value是jsoncpp库中用来表示JSON数据的核心类。它可以存储JSON中的任意数据类型,包括布尔值、数字、字符串、数组和对象。

weixin_42577243的博客 989

还不会用 Json 和 Json-lib?一文教你搞定✅

宝子们👋,今天来唠唠在 Java 开发里超重要的 Json 和 Json-lib,学会了能大大提升开发效率,赶紧码住!

kngines 2664

掌握JSON处理:json-lib库的使用与实践

本文还有配套的精品资源,点击获取 简介:JSON是一种广泛应用于Web应用的数据交换格式, json-lib 库提供了丰富的API来实现Java对象与JSON格式的转换。它支持多种数据类型转换,兼容Java SE和Java EE环境,并且有多个版本以适配不同的Java运行时环境。开发者可以根据项目需求选择合适的 json-lib 版本,并通过其提...

weixin_28931449的博客 1273

json-lib-2.3-jdk15.jar包

Json开发所必需的jar包,以及关联的所以jar包。 列表为: 1)json-lib-2.3-jdk15.jar; 2)ezmorph-1.0.6.jar; 3)commons-logging-1.1.1.jar; 4)commons-lang-2.0.jar; 5)commons-collections-3.1.jar; 6)commons-beanutils.jar.

java中json-lib-jar包的依赖和使用

java中json-lib-jar包的依赖和使用

冷月心的博客 3309

json-lib-2.3-jdk15开发包和所有依赖包

json-lib-2.3-jdk15开发包和所有依赖包

JSON-lib 2.4 JDK15:高效便捷的Java JSON处理库

JSON-lib 2.4 JDK15:高效便捷的Java JSON处理库 【下载地址】JSON-lib2.4JDK15下载 `json-lib-2.4-jdk15.jar` 是一个简单易用的Java JSON库,能够方便地将Java对象与JSON字符串相互转换。该库体积小、无依赖,性能表现出色,适合在各种Java项目中使...

gitblog_09715的博客 1018

红外小目标检测新思路:当DETR遇上Mamba状态空间模型,速度提升3倍

本文探讨了将Mamba状态空间模型与DETR框架结合,用于解决红外小目标检测的难题。该创新方案通过线性复杂度的Mamba模块替代传统Transformer的自注意力机制,在保持高检测精度的同时,显著提升了推理速度,实现了实时性能的突破,为无人机巡检、安防监控等场景提供了高效解决方案。

hhh00的博客 665

以GD32F103C8T6为例的核心板原理图PCB绘制学习笔记简单总结

重点在supplier part是立创商城的编号,可以搜到。

2301_80397661的博客 3262

【Java Web】Ajax —— json-lib包需要依赖的jar包

json-lib包需要依赖的jar包 文章目录1.Json格式的引入2.如何将Java中的对象转换为Json格式3.下载链接 1.Json格式的引入 在Java中,一个对象(某个类的实体)表示如下: public class User { private Integer id; //唯一标识号id private String name; //用户名称name private Double score; //用户分数score //构造函数 pu.

酱懵静的博客 2197

PADS Logic VX2.5元件绘制全流程:从零开始创建自定义元件库

本文详细介绍了在PADS Logic VX2.5中从零开始创建自定义元件库的全流程。内容包括理解PADS独特的四库分立结构、绘制原理图符号(CAE封装)、关联PCB封装、创建元件类型以及最终的调用验证。掌握元件绘制技能能从根本上提升设计效率、确保设计规范统一,并从源头杜绝原理图与PCB之间的关联错误,是硬件工程师必备的核心能力。

oo7890的博客 441

json、javaBean 互相转化 的工具 —— json-lib

从刚开始接触JSON 到现在差不多有两年时间了,最初接触JSON只知道是一种轻量级的数据交换格式。它基于JavaScript的一个子集。后来就很少用到了,后来开始使用Easyui ,就不得不跟Json打交道了。但是也一直没有整理过有关Json的相关知识。直到最近,在做一个项目,前台要求特别高,仅仅是easyui提供的方法已经没有办法满足需求,所以对jquery ,json ,又好好的使用了一次

王勇霞 信息技术提高班九期 3028

手把手教你解决USB批量传输中512字节整数倍数据丢失问题

本文详细解析了USB批量传输中512字节整数倍数据丢失问题的根源,并提供了通过零长度包(ZLP)的解决方案。文章涵盖USB协议核心机制、ZLP实现代码示例(包括Android、Linux和Windows平台)、调试技巧及性能优化建议,帮助开发者彻底解决这一常见问题。

smartcontract5的博客 261

Transformer——从原理、模型架构到内部细节介绍

Transformer模型是一种基于注意力机制的深度学习模型,最早由Google在2017年的论文《AttentionIsAllYouNeed》中提出。自其问世以来就迅速席卷了自然语言处理他(NLP)领域,并在各类主流任务上取得了新的突破,包括机器翻译、语言建模、序列标注和文本分类等。在NLP领域取得巨大成功后,迅速扩展到了计算机视觉(CV)领域,并带来了革命性的变化。这种基于自注意力机制的深度学习模型,不仅在NLP任务中力压群雄,也成为CV领域的新宠。

小拉拉的博客 1万+
上一篇: Myeclipse加载Ext、或者JS失败显示红叉叉的五种解决方案
下一篇: JQuery dataTable 扩展+Ajax Post,Get一些基本操作(一)
lb8607
博客等级 码龄18年 17粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值