Java?從json提取數(shù)組并轉換為list的操作方法
Java 從json提取數(shù)組并轉換為list
這里ret表示json字符串原文
// 解析為JSONObject JSONObject jsonObject = JSONObject.parseObject(ret); // 提取出JSONArray JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("org_list")); // 將JSONArray轉為List列表 String str = JSONObject.toJSONString(jsonArray); List<Org> list = JSONObject.parseArray(str, Org.class);
使用getJSONArray()獲取到jsonarray后,再將jsonArray轉換為字符串,最后將字符串解析為List列表。
代碼中的Org是我List列表中元素的類型。
Java單個對象和List對象轉換成Json,Json轉List
(一)使用單個對象轉換JSON對象
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; import com.css.eshop.exception.DataAccessException; import com.css.eshop.model.VoucherInfo; import com.css.eshop.util.HttpClientUtil; import com.css.eshop.util.LoadStaticReferenceTables; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class TestPut { protected Log logger = LogFactory.getLog(this.getClass().getName()); @Test public void getOneJson(){//測試轉換成Json,單個對象 VoucherInfo vo1=new VoucherInfo(); vo1.setVoucherValue(2131);vo1.setVoucherCode("小可愛的"); JSONObject updateJsonObj = JSONObject.fromObject(vo1);//轉換成json格式 logger.info("---轉換成json格式:---"+updateJsonObj.toString());//提取access_token節(jié)點數(shù)據(jù) } }
輸出轉換后日志:
17-10-2018 17:35 INFO TestPut:35 - ---轉換成json格式:---{"labourCost":0,"refMetalPrice":0,"voucherCoNumber":"","voucherCode":"小可愛的","voucherType":"","voucherValue":2131,"weight":0,"weightUnit":""}
(二)多個對象存到List,再轉換成JSON
@Test public void getArrayList(){//測試轉換成Json,List轉換成JSONList List<VoucherInfo> vouchersList=new ArrayList<VoucherInfo>(); VoucherInfo vo1=new VoucherInfo(); VoucherInfo vo2=new VoucherInfo(); vo1.setVoucherValue(2131);vo1.setVoucherCode("小可愛的"); vo2.setVoucherValue(100);vo2.setVoucherCode("小可愛的222"); vouchersList.add(vo1);vouchersList.add(vo2); JSONArray jsonArray = JSONArray.fromObject(vouchersList); logger.info("--獲取到轉換為json格式的內(nèi)容:"+jsonArray.toString());//提取access_token節(jié)點數(shù)據(jù) }
輸出日志:
17-10-2018 17:36 INFO TestPut:47 - --獲取到轉換為json格式的內(nèi)容:[{"labourCost":0,"refMetalPrice":0,"voucherCoNumber":"","voucherCode":"小可愛的","voucherType":"","voucherValue":2131,"weight":0,"weightUnit":""},{"labourCost":0,"refMetalPrice":0,"voucherCoNumber":"","voucherCode":"小可愛的222","voucherType":"","voucherValue":100,"weight":0,"weightUnit":""}]
(三)json的list對象轉List對象
先轉jsonArray,再轉object提取數(shù)據(jù)
@Test public void test1210() { List<VoucherInfo> vouchers=new ArrayList<VoucherInfo>(); VoucherInfo voucherInfo1=new VoucherInfo();VoucherInfo voucherInfo2=new VoucherInfo(); voucherInfo1.setVoucherCode("coupncdeC0000003");voucherInfo1.setVoucherType("DISC_VOUCHER"); voucherInfo1.setVoucherCoNumber(null);voucherInfo1.setLabourCost(0.0);voucherInfo1.setWeight(0.0); voucherInfo1.setVoucherValue(1000.0);voucherInfo1.setRefMetalPrice(0.0); voucherInfo2.setVoucherCode("coupncdeC0000001");voucherInfo2.setVoucherType("DISC_VOUCHER"); voucherInfo2.setVoucherCoNumber(null);voucherInfo2.setLabourCost(0.0);voucherInfo2.setWeight(0.0); voucherInfo2.setVoucherValue(1000.0);voucherInfo2.setRefMetalPrice(0.0); vouchers.add(voucherInfo1);vouchers.add(voucherInfo2); double ecoupon_amt=0; if(null != vouchers && vouchers.size()>0){ JSONArray vouchersListJsonArray = JSONArray.fromObject(vouchers); String vouchersListJson=vouchersListJsonArray.toString(); System.out.println(vouchersListJson); if(null != vouchersListJson){ JSONArray jsonArray1 = JSONArray.fromObject(vouchersListJson); //循環(huán)獲取json數(shù)組中的 json 對象,然后轉換為 object for (int j = 0; j < jsonArray1.size(); j++) { JSONObject jsonObject2 = jsonArray1.getJSONObject(j); VoucherInfo cust = (VoucherInfo) JSONObject.toBean(jsonObject2, VoucherInfo.class); ecoupon_amt=ecoupon_amt+cust.getVoucherValue(); } } } System.out.println(ecoupon_amt); }
到此這篇關于Java 從json提取出數(shù)組并轉換為list的文章就介紹到這了,更多相關java json提取數(shù)組轉換為list內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
深入淺出重構Mybatis與Spring集成的SqlSessionFactoryBean(上)
通常來講,重構是指不改變功能的情況下優(yōu)化代碼,但本文所說的重構也包括了添加功能。這篇文章主要介紹了重構Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關資料,需要的朋友可以參考下2016-11-11elasticsearch索引index之put?mapping的設置分析
這篇文章主要為大家介紹了elasticsearch索引index之put?mapping的設置分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04idea沒有services窗口、沒有springboot啟動項問題
這篇文章主要介紹了idea沒有services窗口、沒有springboot啟動項問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05