• JsonObject


     

    转自: http://blog.sina.com.cn/s/blog_5bba80460100ean0.html

    Json 必需的包

    commons-httpclient-3.1.jar
    commons-lang-2.4.jar
    commons-logging-1.1.1.jar
    json-lib-2.2.3-jdk13.jar
    ezmorph-1.0.6.jar
    commons-collections-3.2.1.jar

    以上包可以从

    http://commons.apache.org/index.html

    http://json-lib.sourceforge.net/

    http://ezmorph.sourceforge.net/

    http://morph.sourceforge.net/

    http://www.docjar.com/

    中下载到。

    出现 java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher 错误是因为没有导入 ezmorph.jar 文件或版本不对。

    出现 java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap 错误是因为没有导入 commons-collections.jar 文件或版本不对。

    Java 代码转换成 json 代码

    1.        List 集合转换成 json 代码

    List list = new ArrayList();

    list.add( "first" );

    list.add( "second" );

    JSONArray jsonArray2 = JSONArray.fromObject ( list );

    2.        Map 集合转换成 json 代码

    Map map = new HashMap();

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

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

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

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

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

    JSONObject json = JSONObject.fromObject (map);

    3.        Bean 转换成 json 代码

    JSONObject jsonObject = JSONObject.fromObject(new JsonBean());

    4.        数组转换成 json 代码

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

    JSONArray jsonArray1 = JSONArray.fromObject (boolArray);

     

    5. 一般数据转换成 json 代码

    JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );

    6.        beans 转换成 json 代码

    List list = new ArrayList();

    JsonBean2 jb1 = new JsonBean2();

    jb1.setCol(1);

    jb1.setRow(1);

    jb1.setValue( "xx" );

    JsonBean2 jb2 = new JsonBean2();

    jb2.setCol(2);

    jb2.setRow(2);

    jb2.setValue( "" );

    list.add(jb1);

    list.add(jb2);

    JSONArray ja = JSONArray.fromObject (list);

  • 相关阅读:
    CSS
    html5
    XHTML
    HTML
    git 教程 --git revert 命令
    Git 教程 --git merge 命令
    git 教程 --git reset 命令
    git 教程 --git cherry-pick 命令
    git 教程 --git stash命令
    git 教程 --git diff功能
  • 原文地址:https://www.cnblogs.com/hlantian/p/10194632.html
Copyright © 2020-2023  润新知