改变 JSON 对象的值。

huangapple go评论85阅读模式
英文:

change the json object value

问题

{
"onboardingInformation": {
"apiInvokerPublicKey": "abcacabcascvhasj",
"apiInvokerCertificate": "string",
"onboardingSecret": "string"
},
"notificationDestination": "string",
"requestTestNotification": true
}

英文:
{
  "onboardingInformation": {
    "apiInvokerPublicKey": "string",
    "apiInvokerCertificate": "string",
    "onboardingSecret": "string"
  },
  "notificationDestination": "string",
  "requestTestNotification": true
}

I have a json object like above and i want to change apiInvokerPublicKey's value i did not find a method in gson so how can i change it?

{
  "onboardingInformation": {
    "apiInvokerPublicKey": "abcacabcascvhasj",// i want to change just this part
    "apiInvokerCertificate": "string",
    "onboardingSecret": "string"
  },
  "notificationDestination": "string",
  "requestTestNotification": true
}

EDIT: I used addProperty method from gson but it changes whole "onboardingInformation" i just want to change "apiInvokerPublicKey"

答案1

得分: 3

以下是您要翻译的内容:

你可以将整个 JSON 负载读取为 JsonObject 并覆盖现有属性。之后,你可以将其序列化回 JSON

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class GsonApp {

	public static void main(String[] args) throws IOException {
		File jsonFile = new File("./resource/test.json").getAbsoluteFile();

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		JsonObject root = gson.fromJson(Files.newBufferedReader(jsonFile.toPath()), JsonObject.class);
		JsonObject information = root.getAsJsonObject("onboardingInformation");
		information.addProperty("apiInvokerPublicKey", "NEW VALUE");

		String json = gson.toJson(root);

		System.out.println(json);
	}
}

上述代码打印:

{
  "onboardingInformation": {
    "apiInvokerPublicKey": "NEW VALUE",
    "apiInvokerCertificate": "string",
    "onboardingSecret": "string"
  },
  "notificationDestination": "string",
  "requestTestNotification": true
}
英文:

You can read whole JSON payload as JsonObject and overwrite existing property. After that, you can serialise it back to JSON.

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class GsonApp {

	public static void main(String[] args) throws IOException {
		File jsonFile = new File("./resource/test.json").getAbsoluteFile();

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		JsonObject root = gson.fromJson(Files.newBufferedReader(jsonFile.toPath()), JsonObject.class);
		JsonObject information = root.getAsJsonObject("onboardingInformation");
		information.addProperty("apiInvokerPublicKey", "NEW VALUE");

		String json = gson.toJson(root);

		System.out.println(json);
	}
}

Above code prints:

{
  "onboardingInformation": {
    "apiInvokerPublicKey": "NEW VALUE",
    "apiInvokerCertificate": "string",
    "onboardingSecret": "string"
  },
  "notificationDestination": "string",
  "requestTestNotification": true
}

答案2

得分: 0

我正在使用 Jackson API 方法提供代码片段:

// 读取 JSON 并填充 Java 对象
ObjectMapper mapper = new ObjectMapper();
Test test = mapper.readValue(ResourceUtils.getFile("classpath:test.json"), Test.class);

// 进行所需的更改
test.setApiInvokerPublicKey("更新后的值");
// 从 Java 对象写入 JSON
ObjectMapper mapper = new ObjectMapper();
Object value = mapper.writeValue(new File("result.json"), person);
英文:

I am providing code snippet using Jackson api approach

//Read JSON and populate java objects
ObjectMapper mapper = new ObjectMapper();
Test test = mapper.readValue(ResourceUtils.getFile("classpath:test.json") , "Test.class");

//do the required change
test.setApiInvokerPublicKey("updated value");
//Write JSON from java objects
ObjectMapper mapper = new ObjectMapper();
Object value = mapper.writeValue(new File("result.json"), person);

huangapple
  • 本文由 发表于 2020年8月13日 20:54:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63395650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定