Java Background Encapsulation JSON Data Learning Summary (II.)

Source: Internet
Author: User
Tags array to string convert json array to string

First,the application of Jsonarray

Get the corresponding Java array from the JSON array, and if you want to get the elements in the Java array, you only need to traverse the array.

/* * Use the ToArray () method under the corresponding Java array * Jsonarray from the JSON array */jsonarray jsonstrs = new Jsonarray ();         Jsonstrs.add (0, "cat");         Jsonstrs.add (1, "dog");         Jsonstrs.add (2, "bird");         Jsonstrs.add (3, "Duck");                  Object[] Obj=jsonstrs.toarray ();         for (int i=0;i<obj.length;i++) {                System.out.println (obj[i]);         }

A Java array is obtained from a JSON array, which can be transformed, such as converting Jsonarray to string, long, double, integer, date, and so on.
The methods of GetString (index), Getlong (index), getdouble (index) and GetInt (index) under Jsonarray are respectively used.
Similarly, if you want to get the elements in a Java array, you only need to traverse the array.

Import Java.text.simpledateformat;import Java.util.date;import Net.sf.json.jsonarray;public class Test {/* * Get the use of the ToArray () method under the corresponding Java array * Jsonarray from the JSON array * * public static object[] Getjsontoarray (String str) {Jsonarray JS        Onarray = Jsonarray.fromobject (str);  return Jsonarray.toarray (); /** * Convert JSON array to Long type */public static long[] Getjsontolongarray (String str) {Jsonarray Jsonarray = Jsonarra      Y.fromobject (str);      Long[] Arr=new long[jsonarray.size ()];           for (int i=0;i<jsonarray.size (); i++) {Arr[i]=jsonarray.getlong (i);      System.out.println (Arr[i]);  } return arr; /** * Convert JSON array to string type */public static string[] Getjsontostringarray (String str) {Jsonarray Jsonarray = J       Sonarray.fromobject (str);       String[] Arr=new string[jsonarray.size ()];           for (int i=0;i<jsonarray.size (); i++) {arr[i]=jsonarray.getstring (i);       System.out.println (Arr[i]);  } return arr; }/** * Will JThe son array is converted to Double type */public static double[] Getjsontodoublearray (String str) {Jsonarray Jsonarray = Jsonarray.from       Object (str);       Double[] Arr=new double[jsonarray.size ()];       for (int i=0;i<jsonarray.size (); i++) {arr[i]=jsonarray.getdouble (i);  } return arr; /** * Convert JSON array to Date */public static date[] Getjsontodatearray (String jsonstring) {Jsonarray Jsonarray =       Jsonarray.fromobject (jsonstring);       date[] Datearray = new date[jsonarray.size ()];       String datestring;       Date date;       SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");           for (int i = 0; i < jsonarray.size (); i++) {datestring = jsonarray.getstring (i);               try {date=sdf.parse (datestring);           Datearray[i] = date;           } catch (Exception e) {e.printstacktrace ();     }} return Datearray; } public static void Main (string[] args) {Jsonarray jsonlongs = new JSONArray ();     Jsonlongs.add (0, "111");     Jsonlongs.add (1, "222.25");     Jsonlongs.add (2, New Long (333));          Jsonlongs.add (3, 444);     Long[] Log=getjsontolongarray (jsonlongs.tostring ());     for (int i=0;i<log.length;i++) {System.out.println (log[i]);     } Jsonarray jsonstrs = new Jsonarray ();     Jsonstrs.add (0, "2011-01-01");     Jsonstrs.add (1, "2011-01-03");          Jsonstrs.add (2, "2011-01-04 11:11:11");             Date[] D=getjsontodatearray (jsonstrs.tostring ());     for (int i=0;i<d.length;i++) {System.out.println (d[i]); } }}

Run structure:

111222333444Sat Jan 00:00:00 CST 2011Mon Jan 00:00:00 CST 2011Tue Jan 00:00:00 CST 2011

Java Background Encapsulation JSON Data Learning Summary (II.)

Related Article

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.