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

写入JSON文件
EN

Stack Overflow用户
提问于 2020-04-26 10:39:48
回答 1查看 107关注 0票数 1

我正在使用json文件开发一个android应用程序,以存储应用程序使用的数据。我在资产文件夹中有一个Json文件,包括一个对象"plants“。在Dashboard.java文件中,我想向json文件添加一个对象。我尝试过使用put()函数,但是我似乎没有在实际的文件中写入。Dashboard.java:

代码语言:javascript
复制
            String name = intent.getStringExtra(AddAPlant.EXTRA_TEXT1);
            String description = intent.getStringExtra(AddAPlant.EXTRA_TEXT2);
            String url = intent.getStringExtra(AddAPlant.EXTRA_TEXT3);

            JSONObject jsonObj= new JSONObject();

            try {
                jsonObj.put("name", name);
                jsonObj.put("description", description);
                jsonObj.put("cameralink", url);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            plantArray = new JSONArray();
            plantArray.put(jsonObj);

位于资产文件夹中的Json文件:

代码语言:javascript
复制
{
  "plants": [
    {
      "name": "Pepper",
      "decription": "This is a big plant",
      "CameraLink": "https://messir.uni.lu/bicslab/blab-cam1-snapshots/gallery-images/latest.png"
    },
    {
      "name": "Tomatoe",
      "decription": "This is a big plant",
      "CameraLink": "https://messir.uni.lu/bicslab/blab-cam2-snapshots/gallery-images/latest.png"
    },
    {
      "name": "Small Tomato",
      "decription": "It needs a lot of water",
      "CameraLink": "https://messir.uni.lu/bicslab/blab-cam3-snapshots/gallery-images/latest.png"
    }
  ]
}

期望产出:

代码语言:javascript
复制
{
  "plants": [
    {
      "name": "Pepper",
      "decription": "This is a big plant",
      "CameraLink": "https://messir.uni.lu/bicslab/blab-cam1-snapshots/gallery-images/latest.png"
    },
    {
      "name": "Tomatoe",
      "decription": "This is a big plant",
      "CameraLink": "https://messir.uni.lu/bicslab/blab-cam2-snapshots/gallery-images/latest.png"
    },
    {
      "name": "Small Tomato",
      "decription": "It needs a lot of water",
      "CameraLink": "https://messir.uni.lu/bicslab/blab-cam3-snapshots/gallery-images/latest.png"
    }, 
    {
      "name": name,
      "decription": description,
      "CameraLink": url

  ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-26 11:49:23

我不认为在运行时向/assets写入检查这个答案是可能的。

尝试使用应用程序特定的文件文档

对JSON进行更改。从文件(字符串数据)读取并初始化JSONobject。

JSONObject obj =新JSONObject(“文件中的字符串”)

代码语言:javascript
复制
  JSONObject jsonObject = new JSONObject("data from file");
  JSONArray jsonArray =  jsonObject.getJSONArray("plants");

  JSONObject jsonObj = new JSONObject();
  jsonObj.put("name", name);
  jsonObj.put("description", description);
  jsonObj.put("cameralink", url);

  jsonArray = jsonArray.put(jsonObj);
  jsonObject = jsonObject.put("plants", jsonArray);

  //convert json object to string
  String data = jsonObject.toString();

  FileOutputStream fout = context.openFileOutput(filename, Context.MODE_PRIVATE);
  fout.write(data.getBytes());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61439333

复制
相关文章

相似问题

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