2

我在 java 中的 servlet 中有一个字符串作为 json 响应。我怎样才能将它作为列表或任何类型的集合获取。我需要从中创建一个 csv。请帮忙。提前致谢。

"[{\"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"
4

4 回答 4

1
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("--------------------------------------");
    }

输出

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

如果您对此有任何疑问,请告诉我......

于 2013-10-10T12:27:11.397 回答
1

考虑Google GSON库。

它将帮助您解决将这个 JSON 解析为 Java 对象集合的任务。

于 2013-10-10T12:17:07.200 回答
1

搜索这些,你会得到它们: import org.json.JSONArray; 导入 org.json.JSONException;导入 org.json.JSONObject;

于 2013-10-10T12:24:15.330 回答
0

You need to make your custom Class as per your JSON response and then user google GSON liberay

于 2013-10-10T12:18:54.860 回答