我想通过编程将以下数据放入Android Studio中的JSON文件中:
[{
"title":"Olu4a такси",
"logo":"Taxi/olu4a.jpg",
"color":"#FFD916",
"textcolor":"#000000",
"number":"7000",
"bigimage":"Taxi/olu4a_car.jpg",
"star":"4.0"
},
{
"title":"Asia Express такси",
"logo":"Taxi/d.jpg",
"color":"#E7232D",
"textcolor":"#ffffff",
"number":"1616",
"bigimage":"Taxi/asiaexpress.jpg",
"star":"3.5"
}]
怎么办?
示例
JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("year", "3rd"); obj.put("curriculum", "Arts"); obj.put("birthday", "5/5/1993"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
答案 0 :(得分:0)
在将对象序列化为json格式时,通常需要使用JsonWriter
。
如果您要执行的操作只是编写一个包含您在此处编写的特定内容的文件,则可以使用FileWriter
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileWriter writer = new FileWriter(file);
writer.append(jsonString);
writer.flush();
writer.close();
答案 1 :(得分:0)
只需使用FileWriter即可保存到ExternalFilesDir中作为示例
public void save(Context context, String jsonString) throws IOException {
File rootFolder = context.getExternalFilesDir(null);
File jsonFile = new File(rootFolder, "file.json");
FileWriter writer = new FileWriter(jsonFile);
writer.write(jsonString);
writer.close();
//or IOUtils.closeQuietly(writer);
}