0

I use Gson 2.6.2, Java 8. I have file links.json at folder 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"
]

I try to convert the above json file to ArrayList<String>() like this:

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

}

but error:

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)

Help me convert JSON file to ArrayList<String>(), thank you!

2
  • items representing data should be in "key:value" pair, in your JSON you only have values. Not a right way to do things Commented Apr 4, 2016 at 4:38
  • 2
    @PankajNimgade check jsonlint.com Commented Apr 4, 2016 at 4:41

2 Answers 2

4

As you're parsing an array instead of an object, you need to call beginArray() instead of beginObject().

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

while (reader.hasNext()) {
    /* ... */
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer is exactly solution. Wait me press accept button.
Spot-on answer buddy!
1

You actually don't need to explicitly loop over the json data. You can just use the method fromJson like so:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.