简体   繁体   English

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

[英]How to read the json data file I downloaded from firebase as gson?

My json file... I'm sure there is no mistake in Json我的 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"
        }
}

my json file size 1gb.我的 json 文件大小为 1gb。 How do I read this file with that Gson?如何使用 Gson 读取此文件?

My read method..我的阅读方法..

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

the error i got::=> java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NAME at line 1 column 3 path $.我得到的错误::=> java.lang.IllegalStateException: Expected BEGIN_OBJECT 但在第 1 行第 3 列路径 $ 处是 NAME。

My ContactList class That way I cannot read the object.我的联系人列表 class 这样我就无法读取 object。

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

This is a working around fix, but you need to go for each object that you have and add it manually, since you don't know the object name.这是一个解决方法,但是您需要为您拥有的每个 object 设置 go 并手动添加它,因为您不知道 object 名称。

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

I think you can work with this!!我想你可以用这个! I hope you get it running!!我希望你能运行它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM