Reputation: 73
I have developed a tool that can test some requests to a server, the requests themselves are no more than some simple JSON files that are stored on the disc and can be added continuously, but ... there is one more thing, the JSON files contains an e-mail address that needs to be changed upon running the project each time, this is because each of users have a personal e-mail, I made that because server can't accept more than one request from an user. So I am looking for a solution to inject this e-mail address dynamically into JSON.
I'm using Java for this, and also jayway for REST API and Gson for JSONs. So far I looked into google, but can't find anything at all.
Upvotes: 1
Views: 3627
Reputation: 11992
Use Gson.
Gson gson = new Gson();
String yourJsonInStringFormat = " {\"email\":placeHolder,\"password\":\"placeHolder\"}";
Map map = gson.fromJson(yourJsonInStringFormat, Map.class);
map.put("email", "[email protected]");
map.put("password", "123456");
String newJson = gson.toJson(map);
System.out.println(newJson);
This prints out:
{"email":"[email protected]","password":"123456"}
The fields being injected do not need to be there already. For example this also works:
Gson gson = new Gson();
String yourJsonInStringFormat = "{}";
Map map = gson.fromJson(yourJsonInStringFormat, Map.class);
map.put("email", "[email protected]");
map.put("password", "123456");
String newJson = gson.toJson(map);
System.out.println(newJson);
Upvotes: 1
Reputation: 2576
You could do this by these solutions:
jsonTemplate.replace("${e-mail}", email[i])
Upvotes: 3