JavaにおけるJSOnArray、JSOnObject、List、String間の変換について

3362 ワード

一、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

四、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

五.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':':'11 a 11','bbb':'2222','ccc':'3333'},'ccc':'33'},,''''''''''33 33'},'},,'''''''''''''''''''''',"+"{'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);