gpt4 book ai didi

android - 将三个json文件追加到一个json文件中

转载 作者:行者123 更新时间:2023-11-30 00:00:42 32 4
gpt4 key购买 nike

目标:- 我必须将 3 个 json 文件合并为 1 个 json 文件。我需要在从输出文件中删除最后一个字符后将文本附加到输出文件。

注意:- 我这样做是为了将 3 个 json 文件合并到 1 个 json 文件中。我想将文本附加到输出文件中。这段代码附加了文本,但是不要删除最后一个 }。任何人都可以帮我处理代码。提前谢谢你,祝你有美好的一天。

我有一个 json 对象的记录,用于名称、电子邮件、纬度、经度、日期。这些字段位于不同的 json 文件中。我已将它们合并到 1 个 json 文件中。这项工作已完成。

预期输出:-当我追加下一条记录时,我希望 json 文件看起来像

{Record:[{"name1","val","date1","val","lat1","val"..}],[{"name2","val","date2","val","lat2","val"...}]}

实现的输出:-

{Record:[{"name1","val","date1","val","lat1","val"..}],{Record:[{"name2","val","date2","val","lat2","val"...}]}

代码:-

 static class CopyFileContent {

public static void main(String[] args) {

String root = Environment.getExternalStorageDirectory().toString(); //get access to directory path
File myDir = new File(root + "/GeoPark");//create folder in internal storage
myDir.mkdirs();// make directory
File destFile = new File(myDir, FILENAME11);//making a new file in the folder
/* Source file, from which content will be copied */
File sourceFile1 = new File(myDir, FILENAME12);
File sourceFile2 = new File(myDir, FILENAME13);
File sourceFile3 = new File(myDir, FILENAME14);

/* destination file, where the content to be pasted */
// File destFile = new File(FILENAME);

/* if file not exist then create one */
if (!destFile.exists()) {
try {
destFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

InputStream input1 = null;
InputStream input2 = null;
InputStream input3 = null;
OutputStream output = null;
InputStream input4 = null;

try {

/* FileInputStream to read streams */
input1 = new FileInputStream(sourceFile1);
input2 = new FileInputStream(sourceFile2);
input3 = new FileInputStream(sourceFile3);

/* FileOutputStream to write streams */
output = new FileOutputStream(destFile, true);

byte[] buf = new byte[1024];
int bytesRead;
output.write("{Record:[{".getBytes());
while ((bytesRead = input1.read(buf)) > 0) {
output.write(buf, 1, bytesRead);

RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());

}

while ((bytesRead = input2.read(buf)) > 0) {
output.write(buf, 1, bytesRead);
RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());

}

while ((bytesRead = input3.read(buf)) > 0) {
output.write(buf, 1, bytesRead);
RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());
output.write(b.getBytes());
output.write(d.getBytes());
output.write("}]}".getBytes());
RandomAccessFile f1=new RandomAccessFile(destFile,"rw");
long length1= f1.length()-1;
f1.setLength(length1);
f1.close();

output.write(",".getBytes());
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {

if (null != input1) {
input1.close();
}

if (null != input2) {
input2.close();
}

if (null != input3) {
input3.close();
}

if (null != output) {
output.close();
}

} catch (IOException e) {
e.printStackTrace();
}
}

}

}

最佳答案

在某些情况下,您需要深度合并,合并具有相同名称的字段的内容(就像在 Windows 中复制文件夹时一样)。此功能可能会有所帮助:

/**
* Merge "source" into "target". If fields have equal name, merge them recursively.
* @return the merged object (target).
*/
public static JSONObject deepMerge(JSONObject source, JSONObject target) throws JSONException {
for (String key: JSONObject.getNames(source)) {
Object value = source.get(key);
if (!target.has(key)) {
// new value for "key":
target.put(key, value);
} else {
// existing value for "key" - recursively deep merge:
if (value instanceof JSONObject) {
JSONObject valueJson = (JSONObject)value;
deepMerge(valueJson, target.getJSONObject(key));
} else {
target.put(key, value);
}
}
}
return target;
}


public static void main(String[] args) throws JSONException {
JSONObject a = new JSONObject("{offer: {issue1: value1}, accept: true}");
JSONObject b = new JSONObject("{offer: {issue2: value2}, reject: false}");
System.out.println(a+ " + " + b+" = "+JsonUtils.deepMerge(a,b));

}

如果你想合并它们,例如顶级对象有 4 个键(key1、Key2、Key3、Key4),我认为您必须手动执行此操作:

JSONObject merged = new JSONObject(Obj1, JSONObject.getNames(Obj1));
for(String key : JSONObject.getNames(Obj2))
{
merged.put(key, Obj2.get(key));
}

关于android - 将三个json文件追加到一个json文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49993540/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com