如何将json文件转换为ArrayList <string>

时间:2016-04-04 04:36:22

标签: java json arraylist gson

我使用Gson 2.6.2,Java 8.我在文件夹links.json上有文件C:\data\dcv\sportline_java\branches\master\DCV_crawler_engine\src\main\resources\com\dcvsolution\site\bongdaplusdotvn\scrap\links.json

[
   "http://bongdaplus.vn/tin-tuc/ngoi-sao/tin-ben-le/chelsea-giam-gia-ao-pato-de-xa-hang-1494361604.html",
   "http://bongdaplus.vn/tin-tuc/ngoi-sao/wag/bo-sergio-ramos-khong-thich-cuoi-chui-1494221604.html",
   "http://bongdaplus.vn/tin-tuc/ngoi-sao/tin-ben-le/suarez-se-lam-ca-sy-neu-khong-da-bong-1494231604.html",
   "http://bongdaplus.vn/tin-tuc/ngoi-sao/tin-ben-le/10-chuyen-that-nhu-dua-ngay-ca-thang-tu-1179421604.html",
   "http://bongdaplus.vn/tin-tuc/viet-nam/tin-khac/the-he-vang-viet-nam-do-khai-van-vuong-nguoi-tinh-bong-da-1358961604.html",
   "http://bongdaplus.vn/tin-tuc/duc/bundesliga/bundesliga-cang-thang-cuoc-dua-du-champions-league-1492811604.html",
   "http://bongdaplus.vn/tin-tuc/the-gioi/nam-my/argentina/nhan-dinh-bong-da-olimpo-vs-rosario-central-07h15-ngay-2-4-1492801604.html",
   "http://bongdaplus.vn/tin-tuc/the-gioi/nhan-dinh-bong-da-new-england-revolution-vs-new-york-red-bulls-06h00-ngay-2-4-1492791604.html",
   "http://bongdaplus.vn/tin-tuc/anh/hang-nhat-anh/nhan-dinh-bong-da-qpr-vs-m-brough-01h45-ngay-2-4-1492781604.html",
   "http://bongdaplus.vn/tin-tuc/ngoi-sao/tin-ben-le/pique-phan-ung-ra-sao-khi-nghe-ca-khuc-truyen-thong-cua-real-1493481604.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/ibrahimovic-muon-nguoi-paris-tac-tuong-minh-thay-thap-eiffel-1476271603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/ibrahimovic-vuot-cot-moc-100-ban-trong-ngay-psg-vo-dich-ligue-i-1475911603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/li-do-psg-qua-vuot-troi-o-ligue-1-1475781603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/psg-vo-dich-ligue-i-voi-hang-loat-ky-luc-da-va-dang-duoc-tao-lap-1475771603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/psg-vua-cua-cac-vi-vua-1476291603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/ibrahimovic-vuot-cot-moc-100-ban-trong-ngay-psg-vo-dich-ligue-i-1475911603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/li-do-psg-qua-vuot-troi-o-ligue-1-1475781603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/psg-vo-dich-ligue-i-voi-hang-loat-ky-luc-da-va-dang-duoc-tao-lap-1475771603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/vui-dap-troyes-9-0-psg-len-ngoi-vo-dich-ligue-i-som-8-vong-dau-1475901603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/nhan-dinh-bong-da-rennes-vs-lyon-03h00-ngay-14-3-quyet-chien-vi-champions-league-1475501603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/nhan-dinh-bong-da-nantes-vs-angers-23h00-ngay-13-3-1475411603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/marseille-bi-cam-hoa-nice-ap-sat-sat-nhom-dan-dau-1475221603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/nhan-dinh-bong-da-troyes-vs-psg-20h00-ngay-13-3-cho-tiec-vo-dich-1475131603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/psg-co-the-vo-dich-ligue-i-ngay-dem-nay-1475091603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/nhan-dinh-bong-da-bastia-vs-lille-02h00-ngay-13-3-1474551603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/nhan-dinh-bong-da-toulouse-vs-bordeaux-02h00-ngay-13-3-1474561603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/nhan-dinh-bong-da-ajaccio-vs-caen-02h00-ngay-13-3-1474521603.html",
   "http://bongdaplus.vn/tin-tuc/phap/ligue-1/nhan-dinh-bong-da-guingamp-st-etienne-02h00-ngay-13-3-1474501603.html"
]

我尝试将上面的json文件转换为ArrayList<String>(),如下所示:

package com.dcvsolution.site.bongdaplusdotvn.scrap;

import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;

public class JSONparsing {  

    public static void main(String[] args) throws ClassNotFoundException {
        List<String> linksList = new ArrayList<>();
        try {
            //Class<?> cls = Class.forName("JSONparsing.class");
            //ClassLoader classLoader = cls.getClassLoader();
            //JsonReader reader = new JsonReader(new FileReader(classLoader.getResource("links.json").getFile()));
            JsonReader reader = new JsonReader(new FileReader("C:\\data\\dcv\\sportline_java\\branches\\master\\DCV_crawler_engine\\src\\main\\resources\\com\\dcvsolution\\site\\bongdaplusdotvn\\scrap\\links.json"));
            reader.beginObject();
            while (reader.hasNext()) {
                String value = reader.nextString();
                linksList.add(value);
            }
            reader.endObject();
            reader.close();
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        System.out.println(linksList.toString());
    }

}

但错误:

  

线程“main”中的异常java.lang.IllegalStateException:预期   BEGIN_OBJECT但在第1行第2列路径$ at处是BEGIN_ARRAY   com.google.gson.stream.JsonReader.beginObject(JsonReader.java:388)at at   com.dcvsolution.site.bongdaplusdotvn.scrap.JSONparsing.main(JSONparsing.java:22)

帮我转换JSON文件到ArrayList<String>(),谢谢!

2 个答案:

答案 0 :(得分:4)

在解析数组而不是对象时,需要调用beginArray()而不是beginObject()

JsonReader reader = new JsonReader("/* ... */");
reader.beginArray();

while (reader.hasNext()) {
    /* ... */
}

答案 1 :(得分:1)

您实际上不需要显式循环json数据。你可以像这样使用方法fromJson

List<String> list = Arrays.asList(new Gson().fromJson(reader, String[].class));