简体   繁体   English

android-如何从SDCard中的文件解析json

[英]android - how to parse json from file in SDCard

hi i want to parse my downloaded json file that stored in the SDCard directory , 嗨,我想解析我下载的json文件,该文件存储在SDCard目录中,

i don't know how can i do that ! 我不知道该怎么办!

im google a lot but i can only find somethings like : BuffredReader , InputFileStream , ... 我在谷歌上很多,但我只能找到类似的东西:BuffredReader,InputFileStream,...

please help me ! 请帮我 !

here is part of my code but it have the problem i attached in the image : 这是我的代码的一部分,但是有我在图像中附加的问题:

File GroupsJsonFileAdress = new File(Enviroments.getExternalStorageDirectory() + "FOLDER/G.json");

try {
        ObjectInputStream groupsInJson = new ObjectInputStream(new FileInputStream(GroupsJsonFileAdress));
        JSONObject jsonObject = (JSONObject)groupsInJson.readObject();
        String DATABASE_VERSION = jsonObject.getString("DBVersion");
        JSONArray groupsArray = jsonObject.getJSONArray("Groups");
        for (int i = 0; i < groupsArray.length(); i++) {
            JSONObject groupJsonObjectReader = groupsArray.getJSONObject(i);
            int id= groupJsonObjectReader.getInt("Id");
            String gTitle = groupJsonObjectReader.getString("title");
            Group loaderG = new Group();
            loaderG.GroupId = id;
            loaderG.Title = gTitle;
            Log.i("INFO", loaderG.Title);
            groupsClasses.add(loaderG);
        }
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } 

and here is the error i got : http://i.stack.imgur.com/lQ3YN.jpg 这是我得到的错误: http : //i.stack.imgur.com/lQ3YN.jpg

创建一个与您的Json架构匹配的模型,然后使用GSON librarie解析它;)到目前为止,对我来说最简单的方法是:)

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

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