In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to convert data from JSON to JAVA. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
JSON-lib, the Java class package, is used to convert bean,map and XML to JSON and to convert JSON back to bean and DynaBean.
Also need the third party package:
Org.apache.commons (version 3.2 or above)
Org.apache.oro
Net.sf.ezmorph (ezmorph-1.0.4.jar)
Nu.xom
1 、 List
Boolean [] boolArray = new boolean [] {true,false,true}; JSONArray jsonArray1 = JSONArray.fromObject (boolArray); System.out.println (jsonArray1); / / prints [true,false,true] List list = new ArrayList (); list.add ("first"); list.add ("second"); JSONArray jsonArray2 = JSONArray.fromObject (list); System.out.println (jsonArray2) / prints ["first", "second"] JSONArray jsonArray3 = JSONArray.fromObject ("['json','is','easy']"); System.out.println (jsonArray3); / / prints ["json", "is", "easy"] boolean [] boolArray = new boolean [] {true,false,true}; JSONArray jsonArray1 = JSONArray.fromObject (boolArray); System.out.println (jsonArray1) / / prints [true,false,true] List list = new ArrayList (); list.add ("first"); list.add ("second"); JSONArray jsonArray2 = JSONArray.fromObject (list); System.out.println (jsonArray2); / / prints ["first", "second"] JSONArray jsonArray3 = JSONArray.fromObject ("['json','is','easy']") System.out.println (jsonArray3); / / prints ["json", "is", "easy"]
2 、 Map
Map map = new HashMap (); map.put ("name", "json"); map.put ("bool", Boolean.TRUE); map.put ("int", new Integer (1)); map.put ("arr", new String [] {"a", "b"}); map.put ("func", "function (I) {return this.arr [I];}") JSONObject json = JSONObject.fromObject (map); System.out.println (json); / / {"func": function (I) {return this.arr [I];}, "arr": ["a", "b"], "int": 1, "name": "json", "bool": true} Map map = new HashMap (); map.put ("name", "json"); map.put ("bool", Boolean.TRUE) Map.put ("int", new Integer (1)); map.put ("arr", new String [] {"a", "b"}); map.put ("func", "function (I) {return this.arr [I];}"); JSONObject json = JSONObject.fromObject (map); System.out.println (json); / / {"func": function (I) {return this.arr [I] }, "arr": ["a", "b"], "int": 1, "name": "json", "bool": true}
3 、 BEAN
JSONObject jsonObject = JSONObject.fromObject (new JsonBean ()); System.out.println (jsonObject); / / {"func1": function (I) {return this.options [I];}, "pojoId": 1, "name": "json", "options": ["a", "f"], "func2": function (I) {return this.options [I];}} JSONObject jsonObject = JSONObject.fromObject (new JsonBean ()) System.out.println (jsonObject); / / {"func1": function (I) {return this.options [I];}, "pojoId": 1, "name": "json", "options": ["a", "f"], "func2": function (I) {return this.options [I];}}
4 、 BEANS
List list = new ArrayList (); JsonBean2 jb1 = new JsonBean2 (); jb1.setCol (1); jb1.setRow (1); jb1.setValue ("xx"); JsonBean2 jb2 = new JsonBean2 (); jb2.setCol (2); jb2.setRow (2); jb2.setValue ("") List.add (jb1); list.add (jb2); JSONArray ja = JSONArray.fromObject (list); System.out.println (ja.toString ()) / [{"value": "xx", "row": 1, "col": 1}, {"value": "row": 2, "col": 2}] List list = new ArrayList (); JsonBean2 jb1 = new JsonBean2 (); jb1.setCol (1); jb1.setRow (1); jb1.setValue ("xx") JsonBean2 jb2 = new JsonBean2 (); jb2.setCol (2); jb2.setRow (2); jb2.setValue (""); list.add (jb1); list.add (jb2); JSONArray ja = JSONArray.fromObject (list); System.out.println (ja.toString ()) / [{"value": "xx", "row": 1, "col": 1}, {"value": "", "row": 2, "col": 2}]
5 、 String to bean
String json = "{name=\" json\ ", bool:true,int:1,double:2.2,func:function (a) {return a;}, array: [1score2]}"; JSONObject jsonObject = JSONObject.fromString (json); Object bean = JSONObject.toBean (jsonObject); assertEquals (jsonObject.get ("name"), PropertyUtils.getProperty (bean, "name")) AssertEquals (jsonObject.get ("bool"), PropertyUtils.getProperty (bean, "bool"); assertEquals (jsonObject.get ("int"), PropertyUtils.getProperty (bean, "int"); assertEquals (jsonObject.get ("double"), PropertyUtils.getProperty (bean, "double")) AssertEquals (jsonObject.get ("func"), PropertyUtils.getProperty (bean, "func"); List expected = JSONArray.toList (jsonObject.getJSONArray ("array")); assertEquals (expected, (List) PropertyUtils.getProperty (bean, "array")); String json = "{name=\" json\ ", bool:true,int:1,double:2.2,func:function (a) {return a }, array: [1jue 2]} "; JSONObject jsonObject = JSONObject.fromString (json); Object bean = JSONObject.toBean (jsonObject); assertEquals (jsonObject.get (" name "), PropertyUtils.getProperty (bean," name "); assertEquals (jsonObject.get (" bool "), PropertyUtils.getProperty (bean," bool "); assertEquals (jsonObject.get (" int "), PropertyUtils.getProperty (bean," int ")) AssertEquals (jsonObject.get ("double"), PropertyUtils.getProperty (bean, "double"); assertEquals (jsonObject.get ("func"), PropertyUtils.getProperty (bean, "func")); List expected = JSONArray.toList (jsonObject.getJSONArray ("array")); assertEquals (expected, (List) PropertyUtils.getProperty (bean, "array")) String json = "{\" value\ ":\" xx\ ",\" row\ ": 1,\" col\ ": 1}"; JSONObject jsonObject = JSONObject.fromString (json); JsonBean2 bean = (JsonBean2) JSONObject.toBean (jsonObject, JsonBean2.class); assertEquals (jsonObject.get ("col"), new Integer (bean.getCol () AssertEquals (jsonObject.get ("row"), new Integer (bean.getRow ()); assertEquals (jsonObject.get ("value"), bean.getValue ()); String json = "{\" value\ ":\" xx\ ",\" row\ ": 1,\" col\ ": 1}"; JSONObject jsonObject = JSONObject.fromString (json); JsonBean2 bean = (JsonBean2) JSONObject.toBean (jsonObject, JsonBean2.class) AssertEquals (jsonObject.get ("col"), new Integer (bean.getCol ()); assertEquals (jsonObject.get ("row"), new Integer (bean.getRow ()); assertEquals (jsonObject.get ("value"), bean.getValue ())
6 json to xml
1)
JSONObject json = new JSONObject (true); String xml = XMLSerializer.write (json)
< o class="object" null="true">2)
JSONObject json = JSONObject.fromObject ("{\" name\ ":\" json\ ",\" bool\ ": true,\" int\ ": 1}"); String xml = XMLSerializer.write (json)
< o class="object"> < name type="string">Json
< /name> < bool type="boolean">True
< /bool> < int type="number">one
< /int> < /o> < o class="object"> < name type="string">Json
< /name> < bool type="boolean">True
< /bool> < int type="number">one
< /int> < /o>3)
JSONArray json = JSONArray.fromObject ("[1MJ 2je 3]"); String xml = XMLSerializer.write (json)
< a class="array"> < e type="number">one
< /e> < e type="number">two
< /e> < e type="number">three
< /e> < /a>7 、 xml to json
< a class="array"> < e type="function" params="i,j">Return matrix [i] [j]
< /e> < /a> < a class="array"> < e type="function" params="i,j">Return matrix [i] [j]
< /e> < /a>JSONArray json = (JSONArray) XMLSerializer.read (xml); System.out.println (json); / / prints [function (iMagnej) {return matrix [I] [j];}] the above is how to convert data from JSON to JAVA. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.