首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android从url写入json文件

Android从url写入json文件
EN

Stack Overflow用户
提问于 2014-01-30 19:26:08
回答 3查看 941关注 0票数 1

我正在做JSON。我写的代码可以解析JSON并显示列表视图(图像和文本)。现在,我想将我的JSON保存在文件(json.txt)中。这是我的代码。我试图保存JSON,但当我在我的json.txt文件上调试它时,只保存了第一个数据,但我在JSON中有20个数据,如果有人知道解决方案,请帮助......

代码语言:javascript
复制
    jsonparser = new JSONParser();

        JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
        try {

            jsonarray = jsonobject.getJSONArray("data");

            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put("journal", jsonobject.getString(KEY_journal));
                map.put("image", jsonobject.getString(KEY_image));
                map.put("title", jsonobject.getString(KEY_title));
                map.put("description",
                        jsonobject.getString(KEY_description));
                map.put("JournalID", jsonobject.getString(KEY_JournalID));
                map.put("pubDate", jsonobject.getString(KEY_pubDate));
                map.put("statID", jsonobject.getString(KEY_statID));

                Content cont = new Content(jsonobject.getString("journal"),
                        jsonobject.getString("image"),
                        jsonobject.getString("title"),
                        jsonobject.getString("pubDate"),
                        jsonobject.getString("description"),
                        jsonobject.getString("JournalID"),
                        jsonobject.getString("statID"));
                contents.add(cont);




                    yourFile = new File("/sdcard/json.txt");

                    try {
                        writer = new OutputStreamWriter(
                                new FileOutputStream(yourFile), "UTF-8");
                        writer.write(jsonobject.toString());
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (writer != null) {
                            try {
                                writer.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
EN

回答 3

Stack Overflow用户

发布于 2014-01-30 19:32:47

在追加模式下打开文件。

代码语言:javascript
复制
new OutputStreamWriter(new FileOutputStream(yourFile,true), "UTF-8");
票数 0
EN

Stack Overflow用户

发布于 2014-01-30 19:40:28

对从URL中检索到的JSONObject和用于循环数据数组的变量使用单独的变量:

代码语言:javascript
复制
    jsonparser = new JSONParser();

        JSONObject jsonfromurl = jsonparser.getJSONfromURL(URL);
        try {

            jsonarray = jsonfromurl.getJSONArray("data");

            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                HashMap<String, String> map = new HashMap<String, String>();

                map.put("journal", jsonobject.getString(KEY_journal));
                map.put("image", jsonobject.getString(KEY_image));
                map.put("title", jsonobject.getString(KEY_title));
                map.put("description",
                        jsonobject.getString(KEY_description));
                map.put("JournalID", jsonobject.getString(KEY_JournalID));
                map.put("pubDate", jsonobject.getString(KEY_pubDate));
                map.put("statID", jsonobject.getString(KEY_statID));

                Content cont = new Content(jsonobject.getString("journal"),
                        jsonobject.getString("image"),
                        jsonobject.getString("title"),
                        jsonobject.getString("pubDate"),
                        jsonobject.getString("description"),
                        jsonobject.getString("JournalID"),
                        jsonobject.getString("statID"));
                contents.add(cont);




                    yourFile = new File("/sdcard/json.txt");

                    try {
                        writer = new OutputStreamWriter(
                                new FileOutputStream(yourFile), "UTF-8");
                        writer.write(jsonfromurl.toString());
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (writer != null) {
                            try {
                                writer.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
票数 0
EN

Stack Overflow用户

发布于 2014-01-30 19:46:57

您应该遵循与here相同的风格。据我所知,它们实际上将字节写入FileOutputStream,而您尝试写入字符串。根据documentation,FileOutputStream只接受字节。

请尝试使用writer.write(jsonobject.toString().getBytes());

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21455143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档