我是 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
收到回复:
{ 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"
}
]
}
基本上我的要求是更新 json 并作为正文传递给 post 请求
您错误地更改了 中的currentDate
字段responseObject
。您必须jsonObject
在 for 循环内进行修改。
jsonObject.put("currentDate", new Date());
获取数据数组有一个小错误,应该是:
JSONArray dataArray = responseObject.getJSONArray("data");
使用相同的调用 API responseObject
:
Response response = Seller.postSeller(environmentURI, responseObject.toString());
注意:我在currentdate
字段中输入的日期new Date()
只是一个示例,您可以将日期输入所需的格式。
这是运行示例:
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());
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句