我有json文件Called" addplace.json"其内容粘贴在下面。现在我需要阅读上面说的json文件并更改Latitude,longitutde,name,phone,Address的值。那我该怎么做呢,请用示例java代码告诉我。
{
"location": {
"lat": -33.8669710,
"lng": 151.1958750
},
"accuracy": 50,
"name": "Google Shoes!",
"phone_number": "(02) 9374 4000",
"address": "48 Pirrama Road, Pyrmont, NSW 2009, Australia",
"types": ["shoe_store"],
"website": "http://www.google.com.au/",
"language": "en-AU"
}
先谢谢。
答案 0 :(得分:1)
很抱歉忘记了延迟的响应。下面是我在运行时读取json文件,修改它并在后期操作中触发修改后的json的代码
public class Post_Ex3 extends BaseStepClass{
public Scenario scenario;
@Before
public void beforeTest(Scenario scenario)
{
this.scenario =scenario;
}
@When("user changes the attributes values like name {string} , Salary {string} and age {string} in json payload file {string}")
public void user_changes_the_attributes_values_like_name_Salary_and_age_in_json_payload_file(String name, String salary, String age, String payloadfileName) {
String filepath = System.getProperty("user.dir")+"\\src\\test\\resources\\JsonFiles\\"+payloadfileName;
System.out.println("path : " +filepath);
try {
String jsonContents = new String((Files.readAllBytes(Paths.get(filepath))));
JSONObject jsonObject= new JSONObject(jsonContents);
System.out.println(" Post Payload Before changes: ");
System.out.println("============================");
System.out.println(jsonObject.toString(4));
//changing the value of name from tammy to balaji
jsonObject.put("name", name);
jsonObject.put("salary", salary);
jsonObject.put("age", age);
System.out.println(" Post Payload after changes: ");
System.out.println("============================");
System.out.println(jsonObject.toString(4));
String payload = jsonObject.toString(4);
scenario.write("Payload Passed along with post request");
scenario.write(payload);
// Add a header stating the Request body is a JSON
httpRequest.header("Content-Type", "application/json");
// Add the Json to the body of the request
httpRequest.body(jsonObject.toString(4));
} catch (IOException e) {
System.out.println("No file found in the path ");
e.printStackTrace();
}
}
@When("user initiates post request with {string} endpoint")
public void user_initiates_post_request_with_endpoint(String string) {
response = httpRequest.post("/create");
}
@Then("user should get an status code as {string} in post response")
public void user_should_get_an_status_code_as_in_post_response(String expectedStatusCode) {
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, Integer.parseInt(expectedStatusCode),"Failed due to Server issue -"+statusCode);
Assert.assertNotNull(response.asString());
System.out.println("Response :" + response.prettyPrint());
System.out.println("Status Code :" + response.getStatusCode());
System.out.println("Does Reponse contains 'tammy'? :" + response.asString().contains("tammy"));
String name = response.jsonPath().get("data.name").toString();
System.out.println(" name : " +name);
System.out.println(" Salary value in response : " +response.jsonPath().get("data.salary").toString());
}
}