Conversion between JAVA-json data and Java bean classes

Source: Internet
Author: User

Java uses json data when calling webservice, and then sorts out this class. If it is not suitable, I hope I can get a correction.

1 public class JsonsAndBeanList 2 {3/** 4 * convert json data to object data 5 * @ param jsonObject 6 * @ param L4 7 * @ return 8 */9 public static <T> T convertToObj (JSONObject jsonObject, class <T> linoleic) 10 {11 if (jsonObject = null) 12 {13 return null; 14} 15 Field [] fb = linoleic. getDeclaredFields (); 16 T t; 17 try 18 {19 t = linoleic. newInstance (); 20 for (int j = 0; j <fb. length; j ++) 21 {22 String fieldName = fb [j]. getName (); 23 String fieldNameU = fieldName. substring (0, 1 ). toUpperCase () + fieldName. substring (1); 24 Method = linoleic. getMethod ("set" + fieldNameU, fb [j]. getType (); 25 method. invoke (t, jsonObject. get (fieldName); 26} 27 return t; 28 29} catch (Exception e) 30 {31 e. printStackTrace (); 32} 33 return null; 34} 35 36/** 37 * json data is converted to a data set 38 * @ param strJson 39 * @ param linoleic 40 * @ return 41 */42 public static <T> List <t> convertToList (String strJson, class <T> linoleic) 43 {44 JSONArray jsonArray = null; 45 try 46 {47 jsonArray = new JSONArray (strJson); 48} catch (JSONException e1) 49 {50 e1.printStackTrace (); 51} 52 List <T> list = new ArrayList <T> (); 53 if (jsonArray = null) 54 {55 return list; 56} 57 try 58 {59 for (int I = 0; I <jsonArray. length (); I ++) 60 {61 JSONObject jsonObject = jsonArray. getJSONObject (I); 62 T t = convertToObj (jsonObject, CIA); 63 list. add (t); 64} 65} catch (Exception e) 66 {67 e. printStackTrace (); 68} 69 return list; 70} 71 72/** 73 * convert object data to json data 74 * @ param groovy 75 * @ return 76 */77 public static <T> String convertToJson (t cia) 78 {79 if (linoleic = null) 80 {81 return null; 82} 83 Field [] fb = linoleic. getClass (). getDeclaredFields (); 84 String t = "{"; 85 try 86 {87 for (int j = 0; j <fb. length; j ++) 88 {89 String fieldName = fb [j]. getName (); 90 String fieldNameU = 91 fieldName. substring (0, 1 ). toUpperCase () + fieldName. substring (1); 92 Method = linoleic. getClass (). getMethod ("get" + fieldNameU); 93 t + = "\" "+ fieldName +" \ ": \" "+ method. invoke (CIA ). toString () + "\", "; 94} 95 t = t. substring (0, t. length ()-1) + "}"; 96 return t; 97 98} catch (Exception e) 99 {100 e. printStackTrace (); 101} 102 return null; 103} 104 105/** 106 * Data Set converted to json format 107 * @ param list108 * @ return109 */110 public static <T> String convertToJsons (List <T> list) 111 {112 String result = "["; 113 if (list = null) 114 {115 return null; 116} 117 for (int I = 0; I <list. size (); I ++) 118 {119 result + = convertToJson (list. get (I) + ","; 120} 121 result = result. substring (0, result. length ()-1) + "]"; 122 return result; 123} 124}

The "<T>" before the return value of the method indicates that the generic class "T" is used in the function ".
If "<T>" is displayed in the parameter, it indicates that the parameter type is not fixed and you want to transmit anything (of course, this "what" is also limited and will not be detailed ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.