Java:将更新的json作为输入传递给post rest api

Java:将更新的json作为输入传递给post rest api,java,json,Java,Json,我不熟悉Java和放心API。我的要求是,我必须更新收到的json响应,并再次调用相同的API,并将更新后的响应作为该API的输入内容 Response response = Seller.postSeller(environmentURI, someinput); String responseString = response.asString(); JSONObject responseObject = new JSONObject(resp

我不熟悉Java和放心API。我的要求是,我必须更新收到的json响应,并再次调用相同的API,并将更新后的响应作为该API的输入内容

Response response = Seller.postSeller(environmentURI, someinput);
               String responseString = response.asString();

        JSONObject responseObject = new JSONObject(responseString);
                JSONArray dataArray = responseObject.getJSONObject("data");

        for (int i = 0; i < dataArray.length(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);

                       responseObject.put("currentDate", SystemDate.getTodaysDate());
                       }


                       //Again I have to call this API

                       Response response = Seller.postSeller(environmentURI, someinput); 
                       //Here I have update "currentDate": "2019-02-23" to todays date and have to pass that as someinput string

基本上,我的要求是更新json并将post请求作为正文传递给您,因为您错误地更改了responseObject中的currentDate字段。您必须在for循环中修改jsonObject

jsonObject.put("currentDate", new Date());
获取数据数组时出现了一个小错误,应该是:

JSONArray dataArray = responseObject.getJSONArray("data");
使用相同的responseObject调用API:

注意:我在currentdate字段中输入的日期作为新日期只是一个示例,您可以将日期输入所需格式

以下是正在运行的示例:

import org.json.JSONArray;
import org.json.JSONObject;

import java.time.LocalDate;
import java.util.Date;

public class ModifyResponse {
    public static void main(String[] args) {
        //Response response = Seller.postSeller(environmentURI, someinput);
        //String responseString = response.asString();
        String responseString = "{ 'data': [{ 'Id': '1', 'Number': 'G24101457', 'oldDate': '2016-01-01', 'currentDate': '2019-02-23' }, { 'Id': '2', 'Number': 'G24101457', 'oldDate': '2016-01-01', 'currentDate': '2019-02-23' } ] }";
        JSONObject responseObject = new JSONObject(responseString);
        JSONArray dataArray = responseObject.getJSONArray("data");

        for (int i = 0; i < dataArray.length(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            //Here I have update "currentDate": "2019-02-23" to todays date and have to pass that as someinput string
            jsonObject.put("currentDate", new Date());
        }

        System.out.println("Modified data: " + responseObject);
        //Again I have to call this API
        //Response response = Seller.postSeller(environmentURI, responseObject.toString());
    }
}


您正在发布需求、代码和一些JSON,但到目前为止,还没有明确或集中的问题。请帮我们把这个修好。该链接可能会有所帮助。为什么要更改responseObject中的currentDate字段?相反,您必须将jsonObject放入for循环中。在我们的例子中,开发人员已经实现了put-as-post,所以无论何时我们想要调用任何put,我们都会调用post。该api采用类似的json作为输入。所以,当我第一次调用post时,它将保留一些输入json中提供的currentdate。现在,我想再次像put一样调用post api,并且必须通过更新currentDate传递相同的json。我明白,您需要在之前收到的响应中使用更新的currentDate值调用post api。仔细看我的答案。它确实做到了这一点,更新了以前收到的responseObject中的currentDate。现在,通过传递responseObject.toString.See调用POST API,当我第一次调用POST时,它将返回一些json。我要做的是-我必须再次提出一个更新日期的帖子请求。为什么我要根据响应更新日期,原因是在我们的案例中调用更新api post api时,它以相同的JSON体作为输入,我必须从这个响应中改变日期,小子考虑这个更好的方法——我有JSON格式的响应字符串,我必须通过更新里面的日期Field来更新JSON字符串,并且必须通过传递JSON来调用POSTAPI。input@simond以上共享的代码正是您所需要的。1.您调用第一个API并获得“responseString”。2.然后解析这个responseString`并分配给responseObject。3.从responseObject获得数据数组。4.迭代此数据数组,并为数据数组中的每个jsonObject更新currentDate。responseObject会自动更改。因此,您可以通过它包含的jsonObject有效地更改repsonseObject。5.最后,将responseObject转换为字符串JSON格式,并使用它调用下一个API。请运行共享代码。@simond很高兴知道它对您有用。请你接受这个答案好吗?
Response response = Seller.postSeller(environmentURI, responseObject.toString());
import org.json.JSONArray;
import org.json.JSONObject;

import java.time.LocalDate;
import java.util.Date;

public class ModifyResponse {
    public static void main(String[] args) {
        //Response response = Seller.postSeller(environmentURI, someinput);
        //String responseString = response.asString();
        String responseString = "{ 'data': [{ 'Id': '1', 'Number': 'G24101457', 'oldDate': '2016-01-01', 'currentDate': '2019-02-23' }, { 'Id': '2', 'Number': 'G24101457', 'oldDate': '2016-01-01', 'currentDate': '2019-02-23' } ] }";
        JSONObject responseObject = new JSONObject(responseString);
        JSONArray dataArray = responseObject.getJSONArray("data");

        for (int i = 0; i < dataArray.length(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            //Here I have update "currentDate": "2019-02-23" to todays date and have to pass that as someinput string
            jsonObject.put("currentDate", new Date());
        }

        System.out.println("Modified data: " + responseObject);
        //Again I have to call this API
        //Response response = Seller.postSeller(environmentURI, responseObject.toString());
    }
}