如何读取我从firebase下载的json数据文件?

wrrgggsh  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(349)

我的json文件。。。我确信json中没有错误

{
        "-MHG5VdrTky1GdpmNLHl": {
            "author_id": "data1",
            "create_date": 1600162233475,
            "display_name": "data1",
            "phone_number": "data1"
        },
        "-MHG5Vdsy6Hd1zrcRkmm": {
            "author_id": "data1",
            "create_date": 1600162233475,
            "display_name": "data1",
            "phone_number": "data1"
        },
        "-MHG5Vdsy6Hd1zrcRkmn": {
            "author_id": "data1",
            "create_date": 1600162233475,
            "display_name": "data1",
            "phone_number": "data1"
        }
}

我的json文件大小为1gb。我怎么用那个gson读取这个文件?
我的阅读方法。。

InputStream inputStream = new FileInputStream(path);
                Gson gson = new Gson();
                JsonReader reader = new JsonReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                List<ContactList> contactListList = new ArrayList<>();
                reader.beginObject();
                while (reader.hasNext()) {
                    ContactList cts = gson.fromJson(reader, ContactList.class);
    //                contactListList.add(cts);
                }
                reader.endArray();
                reader.close();

我得到的错误::=>java.lang.illegalstateexception:应为begin\u对象,但名称位于第1行第3列路径$。
我的contactlist类无法读取对象。

public class ContactList {

        private String author_id;
        private String display_name;
        private String phone_number;
        private long create_date;

        public ContactList(){}

        public ContactList(String author_id, String display_name, String phone_number, long create_date) {
            this.author_id = author_id;
            this.display_name = display_name;
            this.phone_number = phone_number;
            this.create_date = create_date;
        }

        public String getAuthor_id() {
            return author_id;
        }

        public void setAuthor_id(String author_id) {
            this.author_id = author_id;
        }

        public String getDisplay_name() {
            return display_name;
        }

        public void setDisplay_name(String display_name) {
            this.display_name = display_name;
        }

        public String getPhone_number() {
            return phone_number;
        }

        public void setPhone_number(String phone_number) {
            this.phone_number = phone_number;
        }

        public long getCreate_date() {
            return create_date;
        }

        public void setCreate_date(long create_date) {
            this.create_date = create_date;
        }
    }
uplii1fm

uplii1fm1#

这是一个解决方法,但是您需要为您拥有的每个对象手动添加它,因为您不知道对象名称。

while (reader.hasNext())
    {
        String name = reader.nextName();
        String author_id = "";
        String create_date = "";
        String display_name = "";
        String phone_number = "";

        if (name.equals("author_id"))
        {
            author_id = reader.nextString();
        }
        else if (name.equals("create_date"))
        {
            create_date = String.valueOf(reader.nextLong());
        }
        else if (name.equals("display_name"))
        {
            display_name = reader.nextString();
        }
        else if (name.equals("phone_number"))
        {
            phone_number = reader.nextString();
        }
        else
        {
            reader.skipValue();
        }

        ContactList contactList = new ContactList(author_id, create_date, display_name, 0L);
        contactListArray.add(contactList);
    }

我想你可以用这个!!我希望你能让它运转起来!!

相关问题