我怎样才能在java集合中转换这个json响应的字符串?

时间:2013-10-10 12:15:16

标签: java json parsing collections gson

我在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 个答案:

答案 0 :(得分:1)

考虑Google GSON库。

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

答案 1 :(得分:1)

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

答案 2 :(得分: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
--------------------------------------

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

答案 3 :(得分:0)

您需要根据JSON响应创建自定义类,然后使用用户google GSON liberay