[英]how can i convert this string which is json response in java collection?
I have a string in my servlet in java as a json response. 我在Java的servlet中有一个字符串作为json响应。 how can i get it as list or in any sort of collection. 我如何才能将其作为列表或任何形式的集合获得。 I need to create a csv from this. 我需要从中创建一个csv。 Please help. 请帮忙。 Thanks in advance. 提前致谢。
"[{\"subscriptiontypecode\":\"ELEC\",\"subscriptiontypename\":\"Electricity Billing\",\"billgroupcode\":\"ELEC\",\"billgroupname\":\"Default\",\"billcyclecode\":\"201308\",\"billcyclename\":\"August,2013\",\"unitcode\":\"VGU0000100\",\"unitname\":\"T1/001\",\"totalcharge\":\"2809.00\",\"charge_DHBVNENERGY\":\"1720.00\",\"charge_DGENERGY\":\"233.00\",\"charge_DHBVNMMC\":\"0.00\",\"charge_CAECHARGES\":\"856.00\",\"charge_INTEREST\":\"0.00\",\"charge_ARREARS\":\"0.00\"},{\"subscriptiontypecode\":\"ELEC\",\"subscriptiontypename\":\"Electricity Billing\",\"billgroupcode\":\"ELEC\",\"billgroupname\":\"Default\",\"billcyclecode\":\"201307\",\"billcyclename\":\"July,2013\",\"unitcode\":\"VGU0000100\",\"unitname\":\"T1/001\",\"totalcharge\":\"2566.00\",\"charge_DHBVNENERGY\":\"1699.00\",\"charge_DGENERGY\":\"274.00\",\"charge_DHBVNMMC\":\"0.00\",\"charge_CAECHARGES\":\"593.00\",\"charge_INTEREST\":\"0.00\",\"charge_ARREARS\":\"0.00\"}]\n"
Consider Google GSON library. 考虑使用Google GSON库。
It will help you to solve your task to parse this JSON to a collection of java objects. 它将帮助您解决将JSON解析为Java对象集合的任务。
search for these and you will get them: import org.json.JSONArray; 搜索这些,您将得到它们:import org.json.JSONArray; import org.json.JSONException; 导入org.json.JSONException; import org.json.JSONObject; 导入org.json.JSONObject;
String json = "[{\"subscriptiontypecode\":\"ELEC\",\"subscriptiontypename\":\"Electricity Billing\",\"billgroupcode\":\"ELEC\",\"billgroupname\":\"Default\",\"billcyclecode\":\"201308\",\"billcyclename\":\"August,2013\",\"unitcode\":\"VGU0000100\",\"unitname\":\"T1/001\",\"totalcharge\":\"2809.00\",\"charge_DHBVNENERGY\":\"1720.00\",\"charge_DGENERGY\":\"233.00\",\"charge_DHBVNMMC\":\"0.00\",\"charge_CAECHARGES\":\"856.00\",\"charge_INTEREST\":\"0.00\",\"charge_ARREARS\":\"0.00\"},{\"subscriptiontypecode\":\"ELEC\",\"subscriptiontypename\":\"Electricity Billing\",\"billgroupcode\":\"ELEC\",\"billgroupname\":\"Default\",\"billcyclecode\":\"201307\",\"billcyclename\":\"July,2013\",\"unitcode\":\"VGU0000100\",\"unitname\":\"T1/001\",\"totalcharge\":\"2566.00\",\"charge_DHBVNENERGY\":\"1699.00\",\"charge_DGENERGY\":\"274.00\",\"charge_DHBVNMMC\":\"0.00\",\"charge_CAECHARGES\":\"593.00\",\"charge_INTEREST\":\"0.00\",\"charge_ARREARS\":\"0.00\"}]\n";
JSONArray jsonArray = JSONArray.fromObject( json);
Collection<Map<String,String>> collection = JSONArray.toCollection(jsonArray, HashMap.class);
for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
Map<String, String> mapObject = (Map<String,String>) iterator.next();
System.out.println("------------Starting one record------");
System.out.println("--------------------------------------");
for (Map.Entry<String, String> entry:mapObject.entrySet()) {
System.out.println(entry.getKey() +" = " +entry.getValue());
}
System.out.println("--------------------------------------");
}
OutPut 产量
------------Starting one record------
--------------------------------------
billgroupname = Default
charge_DHBVNMMC = 0.00
unitcode = VGU0000100
unitname = T1/001
charge_INTEREST = 0.00
charge_ARREARS = 0.00
billgroupcode = ELEC
subscriptiontypecode = ELEC
charge_DGENERGY = 233.00
subscriptiontypename = Electricity Billing
charge_CAECHARGES = 856.00
totalcharge = 2809.00
charge_DHBVNENERGY = 1720.00
billcyclecode = 201308
billcyclename = August,2013
--------------------------------------
------------Starting one record------
--------------------------------------
billgroupname = Default
charge_DHBVNMMC = 0.00
unitcode = VGU0000100
unitname = T1/001
charge_INTEREST = 0.00
charge_ARREARS = 0.00
billgroupcode = ELEC
subscriptiontypecode = ELEC
charge_DGENERGY = 274.00
subscriptiontypename = Electricity Billing
charge_CAECHARGES = 593.00
totalcharge = 2566.00
charge_DHBVNENERGY = 1699.00
billcyclecode = 201307
billcyclename = July,2013
--------------------------------------
If you have any doubt on this let me know...... 如果对此有任何疑问,请通知我……
您需要根据JSON响应创建自定义类,然后使用google GSON liberay
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.