关于java中JSONArray、JSONObject、List、String之间的转换

一、JASSONArray转为JSONObject

   JSONArray result_type = new JSONArray();

   StringBuffer cdsIdxType = new StringBuffer();

   cdsIdxType.append(" select id from table_type ");

   result_type = jdbcTemp.queryForJSONArray(cdsIdxType.toString());

  JSONObject jsonObject = (JSONObject) result_type.get(i);

二、JASONArray转为List

  JSONArray result_type = new JSONArray();


   StringBuffer cdsIdxType = new StringBuffer();


   cdsIdxType.append(" select id from table_type ");


   result_type = jdbcTemp.queryForJSONArray(cdsIdxType.toString());


   ArrayList list_type = new ArrayList();


   for (int i = 0; i < result_type.size(); i++) {


    JSONObject jsonObject = (JSONObject) result_type.get(i);


    list_type.add(jsonObject.get("id"));


   }

三、JSONArray转为String

   JSONArray result_type = new JSONArray();


   StringBuffer cdsIdxType = new StringBuffer();


    cdsIdxType.append(" select id from table_type ");


   result_type = jdbcTemp.queryForJSONArray(cdsIdxType.toString());


   String typeAll = "";


   ArrayList list_type = new ArrayList();


   for (int i = 0; i < result_type.size(); i++) {


    JSONObject jsonObject = (JSONObject) result_type.get(i);


    list_type.add(jsonObject.get("id"));
   
   }


   for(int j=0;j<list_type.size();j++){


    typeAll = typeAll + "'" + list_type.get(j) + "'" + ",";


   }


   typeAll = typeAll.substring(0, typeAll.length() -1);

四、String转换为ArrayList

 String tablecode = request.getParameter("tablecode");


  tablecode = tablecode.substring(1, tablecode.length()-1).replace("\"", "");


  String[] list = tablecode.split(",");


  ArrayList tables = new ArrayList();


  for(int i=0; i<list.length; i++){


   tables.add(list[i]);


  }

五.String转JSONObject

String jsonMese = "{"语文":"88","数学":"78","计算机":"99"}";

JSONObject myJson = JSONObject.fromObject(jsonMese);

六.String转JSONArray

String jsonMessage = "[{'num':'成绩', '外语':88, '历史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," + "{'num':'兴趣', '外语':28, '历史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," + "{'num':'爱好', '外语':48, '历史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]";

JSONArray myJsonArray = JSONArray.fromObject(jsonMessage);

七.String转数组

String string = "a,b,c"; String [] stringArr= string.split(","); //注意分隔符是需要转译

如果是"abc"这种字符串,就直接

String string = "abc" ; char [] stringArr = string.toCharArray(); //注意返回值是char数组 如果要返回byte数组就直接使用getBytes方法就ok了

String string = "abc" ; byte [] stringArr = string.getBytes();

八、数组转String

char[] data={a,b,c};

String s=new String(data);

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 最先接触编程的知识是在大学里面,大学里面学了一些基础的知识,c语言,java语言,单片机的汇编语言等;大学毕...
    oceanfive阅读 8,334评论 0 7
  • 晴天多云凉快天,孩子路上笑哈哈, 幼儿园升国旗转圈站,五彩缤纷名族衣, 泰国不过不过儿童节,国旗升完去拜佛, 光脚...
    荣兰_a5f9阅读 1,683评论 0 0
  • 大家现在好,我是耐心,今天把述职PPT交上去了,领导说让修改一下,今天和淘宝合作的项目有了新进展,放到淘宝那边的...
    心羽暖姐姐阅读 1,183评论 0 1
  • 以前没觉得背景调查很重要,但是最近被人事部的姐姐带着屡次长见识,才发现公司的背景调查其实是很有必要的,而且有着决定...
    简约Dr阅读 3,607评论 2 0