java - 如何将 json 文件转换为 ArrayList<String>

标签 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());
    }

}

但是错误:

Exception in thread "main" java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:388) at com.dcvsolution.site.bongdaplusdotvn.scrap.JSONparsing.main(JSONparsing.java:22)

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

最佳答案

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

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

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

关于java - 如何将 json 文件转换为 ArrayList<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36394648/

相关文章:

java - 如何获取数组中的输出值以进一步处理输出

java - 对双数组列表进行插入排序,第一项未排序

javascript - 如何从 Html/JS 中的工具提示复制文本

java - 无法使用 cdi : NullPointerException 注入(inject) bean

java - 尝试反序列化 Json 时出现 JsonMappingException

php - 使用 jQuery Ajax 解析 PHP 对象数组

jquery - 500 错误 - JSON 对象 POST 到 .ASMX Web 服务

java - 在 ArrayList Java 中查找最大随机生成的 double 时出错

java - 继承一个抽象类的多种类型的ArrayList

java - 无效的记住我 token (系列/ token )不匹配。暗示之前的 cookie 盗窃攻击