簡體   English   中英

如何修改現有 Json 文件中的值?

[英]How to Modify values in existing Json File?

我有一個名為“addplace.json”的 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"
}

提前致謝。

抱歉忘了延遲響應。下面是我用來在運行時讀取 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());
        
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM