Use the Jsonarray.fromobject () method and the jar package support required to introduce the Net.sf.json package

Source: Internet
Author: User
Tags string format

about using the Jsonarray.fromobject () method and the jar package support required to introduce the Net.sf.json package.
Net.sf.json's
Latest Version: http://sourceforge.net/projects/json-lib/files/json-lib/
This time select Json-lib-2.3-jdk15.jar version
The latest is the 2.4 version, this time use is Json-lib-2.3-jdk15.jar;
Json-lib also requires the following dependency packages:
Jakarta Commons-lang 2.5
Jakarta commons-beanutils 1.8.0
Jakarta commons-collections 3.2.1
Jakarta commons-logging 1.1.1
Ezmorph 1.0.6

All dependent packages and Json-lib packages have been packaged and uploaded, welcome to download.
Resource Address: http://download.csdn.net/detail/y562363753/9921883

Use the sample:

"[{‘day1‘:‘work‘,‘day2‘:26},{‘day1‘:123,‘day2‘:26}]"; JSONArray jsonArray = JSONArray.fromObject(json);

Actual use:

String allWaterArrayString = req.getParameter("allWaterArrayString");            JSONArray allWaterArray = JSONArray.fromObject(allWaterArrayString);            int count = 0;            List<MonitorAutoEntity> gasList = JSONArray.toList(allWaterArray, new MonitorAutoEntity(), new JsonConfig()); for (MonitorAutoEntity monitorAutoEntity : gasList) { monitorAutoEntity.setPublishstatus("2"); count += this.monitoringInfoService.updateMonitorWaterInfo(monitorAutoEntity); }

The following is quoted http://blog.csdn.net/chenaschen/article/details/41543421 content for easy recording.
First, string and JSON
String format:
static String json = "[{' Day1 ': ' Work ', ' day2 ': 26},{' day1 ': 123, ' Day2 ': 26}]";

Convert to JSON array
Jsonarray Jsonarray = Jsonarray.fromobject (JSON);

Single JSON object transformation
Static String Jobj = {' Day1 ': 1, ' Day2 ': 2};

[Java] View plain copy

JSONObject obj = JSONObject.fromObject(jobj);  

Convert JSON to Java bean

JSONObject jsonObject = JSONObject.fromObject(new JsonBean());  

The List is converted to JSON (map can also)

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);  

Second, iterate through the JSON array

输出每个成员    for(int i=0; i<jsonArray.size(); i++){          System.out.println(jsonArray.get(i));      }  获取每个成员的key及value    JSONObject obj = (JSONObject) jsonArray.get(i);          Iterator it = obj.keys();          while (it.hasNext()) {            String key = it.next().toString();            System.out.println("key ----- "+key);            System.out.println("value ----- "+obj.get(key));      }  

Third, modify, add, delete members

修改涉及到修改json成员的很少,如果真的要修改的话,个人建议转为字符串然后用replaceAll这个函数进行修改,如果你有更好的建议欢迎给评论。增加    JSONObject obj2 = new JSONObject();      obj2.put("day1", "study");      obj2.put("day2", "2");      jsonArray.add(obj2);  删除    jsonArray.remove(index);      jsonArray.subList(fromIndex, toIndex)  

Use the Jsonarray.fromobject () method and the jar package support required to introduce the Net.sf.json package

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.