Parse JSON data using Java

Source: Internet
Author: User

When you want to transmit multiple data in the background on a webpage, sometimes the data is still multiple dynamic lists. when the data is complex, JavaScript programmers like to process them as JSON strings, the backend needs to parse the JSON string after receiving it. Fortunately, there is JSON-Lib. This Java class package is used to convert bean, MAP and XML into JSON and convert JSON back to bean and dynabean.
: Http://json-lib.sourceforge.net/

1 public class test {2 3/** 4 5 * @ Param ARGs 6 7 * @ author Wen 8 9 */10 11 public static void main (string [] ARGs) {12 13 // test1 (); 14 15 // Test2 (); 16 17 string JSON = "{1: {1: {jhinfo: ['scheduler 1 ', 'Baby-friendly ', 'www .wenhq.com'], jhrate: ['1. 0', '6-100:5. 0/1 ']}, 2: {jhinfo: ['Plan 2', 'baby-friendly', 'www .wenhq.com '], jhrate: ['1-100:100. 0 ']}, 3: {jhinfo: ['Plan 3', 'baby-friendly ', 'www .wenhq.com'], jhrate: ['1-100:150. 0/7 '] }}, 2: {4: {jhinfo: ['annual scheduler', 'baby-friendly ', 'www .wenhq.com'], jhrate: ['2017-365: 1000.0 '] }}"; 18 19 try {20 21 jsonobject = jsonobject. fromobject (JSON); 22 23 string name = jsonobject. getstring ("1"); 24 25 string address = jsonobject. getstring ("2"); 26 27 system. out. println ("name is:" + name); 28 29 system. out. println ("address is:" + address); 30 31 iterator it = jsonobject. keys (); 32 33 while (it. hasnext () {34 35 system. out. println (jsonobject. get (it. next (); 36 37} 38 39} catch (jsonexception e) {40 41 E. printstacktrace (); 42 43} 44 45} 46 47/** 48 49 * JSON object String Conversion 50 51 * @ author Wen 52 53 */54 55 Private Static void Test2 () {56 57 string JSON = "{'name': 'Baby-friendly ', 'array': [{'A': '000000',' B ': '000000 ', 'C': '000000'}, {}, {'A': '000000'}], 'address': 'Baby-friendly '}"; 58 59 try {60 61 jsonobject = jsonobject. fromobject (JSON); 62 63 string name = jsonobject. getstring ("name"); 64 65 string address = jsonobject. getstring ("Address"); 66 67 system. out. println ("name is:" + name); 68 69 system. out. println ("address is:" + address); 70 71 jsonarray = jsonobject. getjsonarray ("array"); 72 73 for (INT I = 0; I <jsonarray. size (); I ++) {74 75 system. out. println ("item" + I + ":" + jsonarray. getstring (I); 76 77} 78 79} catch (jsonexception e) {80 81 e. printstacktrace (); 82 83} 84 85} 86 87/** 88 89 * JSON array conversion, array starts with [90 91 * @ author Wen 92 93 */94 95 Private Static void test1 () {96 97 Boolean [] boolarray = new Boolean [] {true, false, true}; 98 99 jsonarray jsonarray1 = jsonarray. fromobject (boolarray); 100 101 system. out. println (jsonarray1); 102 103 // prints [true, false, true] 104 105 list = new arraylist (); 106 107 list. add ("first"); 108 109 list. add ("second"); 110 111 jsonarray jsonarray2 = jsonarray. fromobject (list); 112 113 system. out. println (jsonarray2); 114 115 // prints ["first", "second"] 116 117 jsonarray jsonarray3 = jsonarray. fromobject ("['json', 'is ', 'easy']"); 118 119 system. out. println (jsonarray3); 120 121 // prints ["JSON", "is", "easy"] 122 123}

 

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.