嘿我想知道是否可以编辑现有的json我知道该位置关闭。 所以我们说位置是C:\ Something.json 然后我该如何编辑?。
谢谢
答案 0 :(得分:1)
如果要编辑复杂的JSON结构,我建议使用GSON库。然后,您可以执行以下步骤:
Gson gson = new Gson();
String yourfilecontents; //read contents from File
1)读取JSON文件并将其转换为对象结构。
YourObject obj = gson.fromJson(yourfilecontents, YourObject.class);
2)根据您的要求修改对象。
obj.field1(newValue);
3)将对象转换为JSON
String newJson = gson.toJson(obj);
4)将JSON(newJson)写回文件
答案 1 :(得分:0)
您的问题非常贴心,但这里有一些提示,您可以:
java.io.File
API打开文件答案 2 :(得分:0)
要转换/过滤JSON,尤其是在大文件的情况下,最好将整个文件加载到内存并完全使用对象映射。首选面向流的解析和生成。例如,Gson和Jackson都支持这种技术。为了说明这一点,我使用了小型,快速且无GC的库https://github.com/anatolygudkov/green-jelly。我问了一个问题Loop and edit json file in java,该代码将所有属性名称“ div”重命名为“ b-div”
import org.green.jelly.*;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
public class Transformation {
public static void main(final String[] args) throws IOException {
final String inputContent = "{ \n" +
" \"html\":{ \n" +
" \"child\":[ \n" +
" { \n" +
" \"head\":{ \n" +
"\n" +
" }\n" +
" },\n" +
" { \n" +
" \"body\":{ \n" +
" \"child\":[ \n" +
" { \n" +
" \"div\":{ \n" +
" \"id\":\"intk\",\n" +
" \"class\":\"akagen-row b-table\",\n" +
" \"child\":[ \n" +
" { \n" +
" \"div\":{ \n" +
" \"id\":\"aaaaa\",\n" +
" \"title\":\"bbbbbbb\",\n" +
" \"class\":\"akagen-cell\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" { \n" +
" \"div\":{ \n" +
" \"class\":\"akagen-row b-table\",\n" +
" \"child\":[ \n" +
" { \n" +
" \"div\":{ \n" +
" \"class\":\"akagen-cell\"\n" +
" }\n" +
" },\n" +
" { \n" +
" \"div\":{ \n" +
" \"class\":\"akagen-cell\"\n" +
" }\n" +
" },\n" +
" { \n" +
" \"div\":{ \n" +
" \"class\":\"akagen-cell\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
final Reader input = new StringReader(inputContent); // replace by FileReader
final Writer output = new StringWriter(); // replace by FileWriter
final JsonGenerator generator = new JsonGenerator();
generator.setOutput(new AppendableWriter<>(output));
final JsonParser parser = new JsonParser().setListener(new JsonParserListener() {
@Override
public void onJsonStarted() {
generator.reset();
}
@Override
public void onError(final String error, final int position) {
}
@Override
public void onJsonEnded() {
generator.eoj();
}
@Override
public boolean onObjectStarted() {
generator.startObject();
return true;
}
@Override
public boolean onObjectMember(final CharSequence name) {
if ("div".contentEquals(name)) {
generator.objectMember("b-div");
} else {
generator.objectMember(name);
}
return true;
}
@Override
public boolean onObjectEnded() {
generator.endObject();
return true;
}
@Override
public boolean onArrayStarted() {
generator.startArray();
return true;
}
@Override
public boolean onArrayEnded() {
generator.startArray();
return true;
}
@Override
public boolean onStringValue(final CharSequence data) {
generator.stringValue(data);
return true;
}
@Override
public boolean onNumberValue(final JsonNumber number) {
generator.numberValue(number);
return false;
}
@Override
public boolean onTrueValue() {
generator.trueValue();
return true;
}
@Override
public boolean onFalseValue() {
generator.falseValue();
return true;
}
@Override
public boolean onNullValue() {
generator.nullValue();
return true;
}
});
final CharArrayCharSequence buffer = new CharArrayCharSequence(1024 * 8);
int length;
while ((length = input.read(buffer.getChars())) != -1) {
buffer.setLength(length);
parser.parse(buffer);
}
parser.eoj();
System.out.println(output.toString());
}
}
就CPU利用率和内存占用量而言,这种轻量级转换技术的性能几乎是Java中最高的(没有像JNI上的SIMD这样的技巧)。