Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何转换java集合中的json响应字符串?_Java_Json_Parsing_Collections_Gson - Fatal编程技术网

如何转换java集合中的json响应字符串?

如何转换java集合中的json响应字符串?,java,json,parsing,collections,gson,Java,Json,Parsing,Collections,Gson,我在java的servlet中有一个字符串作为json响应。我怎样才能得到它作为列表或在任何类型的集合。我需要从这个创建一个csv。请帮忙。提前谢谢 "[{\"subscriptiontypecode\":\"ELEC\",\"subscriptiontypename\":\"Electricity Billing\",\"billgroupcode\":\"ELEC\",\"billgroupname\":\"Default\",\"billcyclecode\":\"201308\",\"b

我在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"
以图书馆为例


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

您需要根据JSON响应创建自定义类,然后用户搜索这些对象,您将获得: 导入org.json.JSONArray; 导入org.json.JSONException; 导入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("--------------------------------------");
    }

如果您对此有任何疑问,请让我知道……

您也可以创建一个
新的JSONObject()
(如果它是有效的),循环遍历它并将值分配给MapIs,这是您的代码中的字符串还是真实响应,因为在这种形式下它不是有效的JSon?@Pshemo-its-valid-JSon。我可以根据映射列表进行解析。@Prabhakaran作为一个用代码编写的文本,它是有效的JSon,但如果您将其写入文件并尝试解析它,您将遇到一些问题。
------------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
--------------------------------------