1

我有一个包含 70 个对象的 JSON 文件,每个对象都包含数组等元素。这是 JSON 对象的示例:

{ 
   "journal": ".....",
   "category": ["Sport", "football", "Real Madrid"],
   "abstract": "Here is an example"
} 

首先,我使用元素的字符串创建文件夹"category"。下一步是.txt从元素的字符串创建一个文件"abstract"。我要做的是将每个.txt文件保存在这些文件夹中。

例如,元素"abstract"包含字符串“Here is an example”,我用这个短语创建了一个文件,.txt我想知道如何将它保存在 folderSport和.FootballReal Madrid

4

2 回答 2

1

这是一些示例java:

// create a list of folder names and call it "folderArray".
// You are already doing something like this, but I don't know the variable name.
// You also have the name of the abstract text file name in a variable.
// This code assumes that variable is called "abstractFileName".

for (final String folder : folderArray)
{
    final String newFileName = folder + File.separatorChar + abstractFileName;

    // create a file with the name "newFileName"

    // Write the abstract contents to the new file.
}
于 2016-03-28T16:08:07.080 回答
0
JSONObject obj = new JSONObject(jsonString);
JSONArray arr = obj.getJSONArray("category");
File f = new File("C:/Files");
for(int i = 0; i < arr.length; i++)
{
  String folderName = arr.getString(i);
  File folder = new File(folderName);
  if(folder.mkdir()) {
    File file = new File(folderName + "fileName.txt");
    String data = obj.getString("abstract");

    // Now using stream write the data to this file
    DataOutputStream dos = new DataOutputStream(FileOutputStream(file));
    dos.writeUTF(data);
}
于 2016-03-28T17:20:07.273 回答