java操作JSON資料格式__JSON

來源:互聯網
上載者:User

需要引入的包:

jakarta commons-lang 2.3 jakarta commons-beanutils 1.7.0 jakarta commons-collections 3.2 jakarta commons-logging 1.1 ezmorph 1.0.4 json-lib-2.4-jdk15.jar


Product.java

public class Product {private String product;private double price;private Object item;public Object getItem() {return item;}public void setItem(Object item) {this.item = item;}public String getProduct() {return product;}public void setProduct(String product) {this.product = product;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}}


JsonUtil.java

public class JsonUtil {// {"product":"pencil","price":12,"item":[{"id":1,"name":"chen"}]}private static final String jsoStr = "{\"product\":\"pencil\",\"price\":12,\"item\":[{\"id\":1,\"name\":\"chen\"}]}";public static void main(String[] args) throws IOException {// StringSystem.out.println(string4json("key", "20"));// objProduct p = (Product) getObject4JsonString(jsoStr, Product.class);System.out.println(p.getProduct() + "," + p.getPrice());// mapMap map = getMap4Json(jsoStr);for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {Map.Entry entry = (Map.Entry) iterator.next();System.out.println(entry.getKey() + "," + entry.getValue());}// 數組Object[] objects = getStringArray4Json(jsoStr);for (int i = 0; i < objects.length; i++) {System.out.println(objects[i]);}}// 將String轉換成JSONpublic static String string4json(String key, String value) throws JSONException {JSONObject object = new JSONObject();object.put(key, value);return object.toString();}// 從一個JSON 對象字元格式設定中得到一個java對象public static Object getObject4JsonString(String jsonString, Class pojoCalss) {Object pojo;JSONObject jsonObject = JSONObject.fromObject(jsonString);pojo = JSONObject.toBean(jsonObject, pojoCalss);return pojo;}// 從json 運算式中擷取一個mappublic static Map getMap4Json(String jsonString) {JSONObject jsonObject = JSONObject.fromObject(jsonString);Iterator keyIter = jsonObject.keys();String key;Object value;Map valueMap = new HashMap();while (keyIter.hasNext()) {key = (String) keyIter.next();value = jsonObject.get(key);valueMap.put(key, value);}return valueMap;}// 從json對象集合運算式中得到一個java對象列表public static List getList4Json(String jsonString, Class pojoClass) {JSONArray jsonArray = JSONArray.fromObject(jsonString);JSONObject jsonObject;Object pojoValue;List list = new ArrayList();for (int i = 0; i < jsonArray.size(); i++) {jsonObject = jsonArray.getJSONObject(i);pojoValue = JSONObject.toBean(jsonObject, pojoClass);list.add(pojoValue);}return list;}// 從json數組中解析出java字串數組public static Object[] getStringArray4Json(String jsonString) {JSONObject jsonObj = JSONObject.fromObject(jsonString);JSONArray jsonarr = jsonObj.getJSONArray("item");return (Object[]) jsonarr.toArray();}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.