如何将Json对象附加到android中已有的Json文件中

时间:2021-01-09 14:01:12

My json file contains,

我的json文件包含,

{
  "appointments": [
    {
      "appointmentId": "app_001",
      "appointmentTitle": "Appointment Title1",
      "appointmentDate": "2017-11-25",
      "appointmentTime": "10:30",
      "appointmentStatus": "active",
      "appointmentType": "meeting",
      "reminder": {
        "type": "notification",
        "time": "10:15",
        "status": "off"
      },
      "appointmentDescription": "blablablablabla1"
    },
    {
      "appointmentId": "app_002",
      "appointmentTitle": "AppointmentTitle2",
      "appointmentDate": "2017-11-26",
      "appointmentTime": "09:00",
      "appointmentStatus": "done",
      "appointmentType": "exam",
      "reminder": {
        "type": "alarm",
        "time": "08:45",
        "status": "on"
      },
      "appointmentDescription": "blablablablabla2"
    }
  ]
}

I need to put another jsonobject into array, Out put should be like,

我需要把另一个jsonobject放到数组中,输出应该是,

{
  "appointments": [
    {
      "appointmentId": "app_001",
      "appointmentTitle": "Appointment Title1",
      "appointmentDate": "2017-11-25",
      "appointmentTime": "10:30",
      "appointmentStatus": "active",
      "appointmentType": "meeting",
      "reminder": {
        "type": "notification",
        "time": "10:15",
        "status": "off"
      },
      "appointmentDescription": "blablablablabla1"
    },
    {
      "appointmentId": "app_002",
      "appointmentTitle": "AppointmentTitle2",
      "appointmentDate": "2017-11-26",
      "appointmentTime": "09:00",
      "appointmentStatus": "done",
      "appointmentType": "exam",
      "reminder": {
        "type": "alarm",
        "time": "08:45",
        "status": "on"
      },
      "appointmentDescription": "blablablablabla2"
    },
    {
      "appointmentId": "app_003",
      "appointmentTitle": "AppointmentTitle3",
      "appointmentDate": "2017-11-26",
      "appointmentTime": "09:00",
      "appointmentStatus": "done",
      "appointmentType": "exam",
      "reminder": {
        "type": "alarm",
        "time": "08:45",
        "status": "on"
      },
      "appointmentDescription": "blablablablabla3"
    }
  ]
}

I used following code segment perform my requirement.

我使用以下代码段执行我的需求。

File fileJson = new File(getApplicationContext().getExternalFilesDir("/app"), "app.json");

String strFileJson = getStringFromFile(fileJson.toString());

JSONObject jsonObj = new JSONObject(strFileJson);

jsonObj.put("appointmentId", "app_002");
jsonObj.put("appointmentTitle", "Appointment Title2");
jsonObj.put("appointmentDate", "2017-11-21");
jsonObj.put("appointmentTime", "01:30");
jsonObj.put("appointmentStatus", "active");
jsonObj.put("appointmentType", "meeting");

JSONObject reminder = new JSONObject();
reminder.put("type", "note");
reminder.put("time", "12:30");
reminder.put("status", "off");
jsonObj.put("reminder", reminder);
jsonObj.put("appointmentDescription", "blablablablabla2");

writeJsonFile(fileJson, jsonObj.toString()); 

writeJsonFile, getStringFromFile, convertStreamToString functions are,

writeJsonFile getStringFromFile convertStreamToString函数是,

public static String getStringFromFile(String filePath) throws Exception {
    File fl = new File(filePath);
    FileInputStream fin = new FileInputStream(fl);
    String ret = convertStreamToString(fin);
    //Make sure you close all streams.
    fin.close();
    return ret;
}

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    return sb.toString();
}

public static void writeJsonFile(File file, String json) {
    BufferedWriter bufferedWriter = null;
    try {

        if (!file.exists()) {
            Log.e("App","file not exist");
            file.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(file);
        bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(json);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bufferedWriter != null) {
                bufferedWriter.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

but the output I am getting is,

但是我得到的输出是,

{
  "appointments": [
    {
      "appointmentId": "app_001",
      "appointmentTitle": "Appointment Title1",
      "appointmentDate": "2017-11-25",
      "appointmentTime": "10:30",
      "appointmentStatus": "active",
      "appointmentType": "meeting",
      "reminder": {
        "type": "notification",
        "time": "10:15",
        "status": "off"
      },
      "appointmentDescription": "blablablablabla1"
    },
    {
      "appointmentId": "app_002",
      "appointmentTitle": "AppointmentTitle2",
      "appointmentDate": "2017-11-26",
      "appointmentTime": "09:00",
      "appointmentStatus": "done",
      "appointmentType": "exam",
      "reminder": {
        "type": "alarm",
        "time": "08:45",
        "status": "on"
      },
      "appointmentDescription": "blablablablabla2"
    }
  ],
  "appointmentId": "app_002",
  "appointmentTitle": "Appointment Title2",
  "appointmentDate": "2017-11-21",
  "appointmentTime": "01:30",
  "appointmentStatus": "active",
  "appointmentType": "meeting",
  "reminder": {
    "type": "note",
    "time": "12:30",
    "status": "off"
  },
  "appointmentDescription": "blablablablabla2"
}

Please help me to get required format of json as output. Thanks in advance

请帮我得到json的要求格式作为输出。谢谢提前

4 个解决方案

#1


0  

Hope this would do what you want, Replace your

希望这能做你想做的,取代你的

JSONObject jsonObj = new JSONObject(strFileJson);

jsonObj.put("appointmentId", "app_002");
jsonObj.put("appointmentTitle", "Appointment Title2");
jsonObj.put("appointmentDate", "2017-11-21");
jsonObj.put("appointmentTime", "01:30");
jsonObj.put("appointmentStatus", "active");
jsonObj.put("appointmentType", "meeting");

JSONObject reminder = new JSONObject();
reminder.put("type", "note");
reminder.put("time", "12:30");
reminder.put("status", "off");
jsonObj.put("reminder", reminder);
jsonObj.put("appointmentDescription", "blablablablabla2");

with this,

用这个,

JSONObject PreviousJsonObj = new JSONObject(strFileJson);
JSONArray array = PreviousJsonObj.getJSONArray("appointments");
JSONObject jsonObj= new JSONObject();

jsonObj.put("appointmentId", "app_002");
jsonObj.put("appointmentTitle", "Appointment Title2");
jsonObj.put("appointmentDate", "2017-11-21");
jsonObj.put("appointmentTime", "01:30");
jsonObj.put("appointmentStatus", "active");
jsonObj.put("appointmentType", "meeting");

JSONObject reminder = new JSONObject();
reminder.put("type", "note");
reminder.put("time", "12:30");
reminder.put("status", "off");
jsonObj.put("reminder", reminder);
jsonObj.put("appointmentDescription", "blablablablabla2");
array.put(jsonObj);

JSONObject currentJsonObject = new JSONObject();
currentJsonObject.put("appointments",array);

#2


1  

You are almost on the right track. you just have to add the JSONObject inside your JSONArray. Try this

你几乎是在正确的轨道上。只需在JSONArray中添加JSONObject即可。试试这个

    JSONObject OldJsonObj = new JSONObject(strFileJson);
    JSONArray array = OldJsonObj.getJSONArray("appointments");
    JSONObject jsonObj= new JSONObject();


jsonObj.put("appointmentId", "app_002");
jsonObj.put("appointmentTitle", "Appointment Title2");
jsonObj.put("appointmentDate", "2017-11-21");
jsonObj.put("appointmentTime", "01:30");
jsonObj.put("appointmentStatus", "active");
jsonObj.put("appointmentType", "meeting");

JSONObject reminder = new JSONObject();
reminder.put("type", "note");
reminder.put("time", "12:30");
reminder.put("status", "off");
jsonObj.put("reminder", reminder);
jsonObj.put("appointmentDescription", "blablablablabla2");
array.put(jsonObj);  // put the data in array

JSONObject newJsonObject = new JSONObject(array.toString());

writeJsonFile(fileJson, newJsonObject .toString()); 

#3


0  

You can use FileWriter to write in text files. Use below code:

您可以使用FileWriter来编写文本文件。使用下面的代码:

try{ 

   FileWriter fileWriter = new FileWriter(Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.StampWallet/" + "SBLog.txt", true); 

   fileWriter.write("Hello");

   fileWrite.close(); 

}catch(IOException e){ 

}

know more about it visit here

了解更多关于它的访问。

You can use Gson to convert object in string or vice-versa.

您可以使用Gson在字符串中转换对象,或者相反。

#4


0  

You will have to add the JSONObject inside your JSONArray appointments, try this

您将不得不在JSONArray约会中添加JSONObject,请尝试此操作

    JSONObject jsonObject=new JSONObject(strFileJson);
    JSONObject jsonObj=new JSONObject();
    JSONObject jObj=new JSONObject();


    try {
        jsonObj.put("appointmentId", "app_002");
        jsonObj.put("appointmentTitle", "Appointment Title2");
        jsonObj.put("appointmentDate", "2017-11-21");
        jsonObj.put("appointmentTime", "01:30");
        jsonObj.put("appointmentStatus", "active");
        jsonObj.put("appointmentType", "meeting");
        jObj.put("type", "note");
        jObj.put("time", "12:30");
        jObj.put("status", "off");
        jsonObj.put("reminder",jObj);
        JSONArray jsonArray=jsonObject.getJSONArray("appointments");
        jsonArray.put(jsonObj);
    } catch (JSONException e) {
        e.printStackTrace();
    }

#1


0  

Hope this would do what you want, Replace your

希望这能做你想做的,取代你的

JSONObject jsonObj = new JSONObject(strFileJson);

jsonObj.put("appointmentId", "app_002");
jsonObj.put("appointmentTitle", "Appointment Title2");
jsonObj.put("appointmentDate", "2017-11-21");
jsonObj.put("appointmentTime", "01:30");
jsonObj.put("appointmentStatus", "active");
jsonObj.put("appointmentType", "meeting");

JSONObject reminder = new JSONObject();
reminder.put("type", "note");
reminder.put("time", "12:30");
reminder.put("status", "off");
jsonObj.put("reminder", reminder);
jsonObj.put("appointmentDescription", "blablablablabla2");

with this,

用这个,

JSONObject PreviousJsonObj = new JSONObject(strFileJson);
JSONArray array = PreviousJsonObj.getJSONArray("appointments");
JSONObject jsonObj= new JSONObject();

jsonObj.put("appointmentId", "app_002");
jsonObj.put("appointmentTitle", "Appointment Title2");
jsonObj.put("appointmentDate", "2017-11-21");
jsonObj.put("appointmentTime", "01:30");
jsonObj.put("appointmentStatus", "active");
jsonObj.put("appointmentType", "meeting");

JSONObject reminder = new JSONObject();
reminder.put("type", "note");
reminder.put("time", "12:30");
reminder.put("status", "off");
jsonObj.put("reminder", reminder);
jsonObj.put("appointmentDescription", "blablablablabla2");
array.put(jsonObj);

JSONObject currentJsonObject = new JSONObject();
currentJsonObject.put("appointments",array);

#2


1  

You are almost on the right track. you just have to add the JSONObject inside your JSONArray. Try this

你几乎是在正确的轨道上。只需在JSONArray中添加JSONObject即可。试试这个

    JSONObject OldJsonObj = new JSONObject(strFileJson);
    JSONArray array = OldJsonObj.getJSONArray("appointments");
    JSONObject jsonObj= new JSONObject();


jsonObj.put("appointmentId", "app_002");
jsonObj.put("appointmentTitle", "Appointment Title2");
jsonObj.put("appointmentDate", "2017-11-21");
jsonObj.put("appointmentTime", "01:30");
jsonObj.put("appointmentStatus", "active");
jsonObj.put("appointmentType", "meeting");

JSONObject reminder = new JSONObject();
reminder.put("type", "note");
reminder.put("time", "12:30");
reminder.put("status", "off");
jsonObj.put("reminder", reminder);
jsonObj.put("appointmentDescription", "blablablablabla2");
array.put(jsonObj);  // put the data in array

JSONObject newJsonObject = new JSONObject(array.toString());

writeJsonFile(fileJson, newJsonObject .toString()); 

#3


0  

You can use FileWriter to write in text files. Use below code:

您可以使用FileWriter来编写文本文件。使用下面的代码:

try{ 

   FileWriter fileWriter = new FileWriter(Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.StampWallet/" + "SBLog.txt", true); 

   fileWriter.write("Hello");

   fileWrite.close(); 

}catch(IOException e){ 

}

know more about it visit here

了解更多关于它的访问。

You can use Gson to convert object in string or vice-versa.

您可以使用Gson在字符串中转换对象,或者相反。

#4


0  

You will have to add the JSONObject inside your JSONArray appointments, try this

您将不得不在JSONArray约会中添加JSONObject,请尝试此操作

    JSONObject jsonObject=new JSONObject(strFileJson);
    JSONObject jsonObj=new JSONObject();
    JSONObject jObj=new JSONObject();


    try {
        jsonObj.put("appointmentId", "app_002");
        jsonObj.put("appointmentTitle", "Appointment Title2");
        jsonObj.put("appointmentDate", "2017-11-21");
        jsonObj.put("appointmentTime", "01:30");
        jsonObj.put("appointmentStatus", "active");
        jsonObj.put("appointmentType", "meeting");
        jObj.put("type", "note");
        jObj.put("time", "12:30");
        jObj.put("status", "off");
        jsonObj.put("reminder",jObj);
        JSONArray jsonArray=jsonObject.getJSONArray("appointments");
        jsonArray.put(jsonObj);
    } catch (JSONException e) {
        e.printStackTrace();
    }