Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修改现有Json文件中的值?_Java_Json_Rest_Rest Assured - Fatal编程技术网

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

Java 如何修改现有Json文件中的值?,java,json,rest,rest-assured,Java,Json,Rest,Rest Assured,我有一个名为“addplace.json”的json文件,其内容粘贴在下面。现在我需要阅读上面提到的json文件,并更改纬度、经度、姓名、电话和地址的值。那么我将如何做到这一点,请用示例java代码告诉我 { "location": { "lat": -33.8669710, "lng": 151.1958750 }, "accuracy": 50, "name": "Google Shoes!", "phone_number": "(02) 9374 400

我有一个名为“addplace.json”的json文件,其内容粘贴在下面。现在我需要阅读上面提到的json文件,并更改纬度、经度、姓名、电话和地址的值。那么我将如何做到这一点,请用示例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文件、修改它并作为post操作的一部分触发修改后的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());
        
}

}

你应该先尝试一下,有很多教程介绍如何按照你的要求去做。如果你陷入困境,那么就回来分享你失败的代码。到目前为止你都尝试了什么?你能给我们举一些例子吗?在目前的形式下,这个问题不适合这样做;定义您的位置和地点类;从json反序列化;编辑您的数据;然后重新序列化为json格式?您能执行以下哪一个步骤?@NiVer我已尝试并寻求解决方案。我没有把代码贴在这里,这是我的错。下面是我尝试过的代码段。File jsonFile=new File(System.getProperty(“user.dir”)+“//resources//”+addPlace\u Payload.json);if(jsonFile.exists()){ObjectMapper mapper=new ObjectMapper();ObjectReader reader=mapper.reader();String jsonString=FileUtils.readFileToString(jsonFile);JsonNode节点=reader.readTree(jsonString);System.out.println(node.toString());ObjectNode ObjectNode ObjectNode=(ObjectNode)node;ObjectNode.put(“location.lat”,“-55.8669710”);objectNode.put(“name”,“Mysuru”);System.out.println(node.toString());}