Java基礎之FastJson詳解
一、fastJson將json格式字符串轉化成List集合
註:json格式字符串必須符合數組型格式如[{“a”:a},{“b”:b}]
場景一:前端向後臺傳遞數組格式的json字符串,如何轉化成List集合
List<AccountBean> readJson2List =JSON.parseArray(json, AccountBean.class)註意這裡是Bean.class而不是List.class
@Test public void readJson2ListBean() { String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; List<AccountBean> readJson2List =JSON.parseArray(json, AccountBean.class); //輸出測試 JsonUtils.beanToJson(readJson2List); }
場景二:前端向後臺傳遞數組格式的json字符串,如何轉化成List<HashMap<String,Object>>集合
@Test public void readJson2ListMap() { String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; List<HashMap> readJson2List =JSON.parseArray(json,HashMap.class); //輸出測試 JsonUtils.beanToJson(readJson2List); }
二、fastJson將json格式字符串轉化成對象
場景一:前端向後臺傳遞數組格式的json字符轉化成Bean對象
@Test public void readJson2Bean() { String json = "{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}"; AccountBean accountBean =JSON.parseObject(json,AccountBean.class); //輸出測試 JsonUtils.beanToJson(accountBean); }
場景二:前端向後臺傳遞數組格式的json字符串Map<String,Object>集合
@Test public void readJson2Map() { String json = "{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}"; HashMap<String,Object> accountBean =JSON.parseObject(json,Map.class); //輸出測試 JsonUtils.beanToJson(accountBean); }
測試:
三、FastJson將對象或集合轉化成json格式字符串
@Test public void beanAndCollectionTOJson() { AccountBean bean = new AccountBean(); bean.setAddress("china-Guangzhou"); bean.setEmail("[email protected]"); bean.setId(1); bean.setName("hoojo"); String json = JSON.toJSONString(bean); System.out.println(json); }
四、FastJson將json格式字符串轉化成json對象
@Test public void jsonStringTOJsonBean() { String json = "{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}"; JSONObject parse = (JSONObject) JSON.parse(json); String address = (String) parse.get("address"); System.out.println("輸出address=="+address); }
五、FastJson將json格式字符串轉化成json數組
@Test public void jsonString2JsonArray() { String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; JSONArray parseArray = JSON.parseArray(json); //輸出測試 JsonUtils.beanToJson(parseArray.get(0)); }
六、FastJson將java對象轉化成json對象
@Test public void beanAndCollectionTOJsonBean() { AccountBean bean = new AccountBean(); bean.setAddress("china-Guangzhou"); bean.setEmail("[email protected]"); bean.setId(1); bean.setName("hoojo"); JSONObject json = (JSONObject) JSON.toJSON(bean); json.get("address"); System.out.println(json); }
到此這篇關於Java基礎之FastJson詳解的文章就介紹到這瞭,更多相關Java FastJson內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- java FastJson的簡單用法
- Java使用fastjson對String、JSONObject、JSONArray相互轉換
- java比較兩個json文件的差異及說明
- fastjson轉換對象實體@JsonProperty不生效問題及解決
- 如何修改JSON字符串中的敏感信息