gpt4 book ai didi

java - JSONObject 未正确更新

转载 作者:行者123 更新时间:2023-12-02 09:31:03 25 4
gpt4 key购买 nike

我正在更新 JSONArray 内的 JSONObject 中的值,当更改值时,会更新所有 JSONArray 中的值。有谁知道为什么吗?

 public static void uploadMediaWithThumbnail( final LeagueActivity.UploadingCallback call,
final long leagueId,
final JSONArray information, final JsonHttpResponseHandler handler) {


final AtomicInteger receivedCount = new AtomicInteger();
receivedCount.set(0);

call.progressCall(10);

getMediaUploadUrl(leagueId, information, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
call.progressCall(20);

final JSONArray allData = response.getJSONArray("upload_data");
AsyncHttpClient[] clients = new AsyncHttpClient[allData.length()*2];
JSONArray jarr = information;

for (int i = 0; i < allData.length(); i++ ) {
final String uploadUrl = allData.getJSONObject(i).getString("content_url");
final String previewUrl = allData.getJSONObject(i).getString("preview_url");

jarr.getJSONObject(i).put("content", uploadUrl);
jarr.getJSONObject(i).put("preview", previewUrl);

}

final JSONArray newInfo = shallowCopy(jarr);

Log.d("Log1", newInfo.getJSONObject(1).getString("content"));

Log.d("Log2", newInfo.getJSONObject(0).getString("content"));

记录 Log1 和 Log2 时,它们包含相同的链接

信息是这样的数据[{"type":"video","format":"mp4","preview_format":"jpg"}, {"type":"video","format ":"mp4","preview_format":"jpg"}]

AllData 是从 REST HTTP 调用接收到的信息,并且与信息的长度相同

最佳答案

感谢您的更新。

您想要将信息项添加到 JSONObject 中,然后将该对象添加到 JSONArray 中。

    JSONArray jarr = information;

for (int i = 0; i < allData.length(); i++ ) {
final String uploadUrl = allData.getJSONObject(i).getString("content_url");
final String previewUrl = allData.getJSONObject(i).getString("preview_url");

JSONObject object = new JSONObject();
object.put("content", uploadUrl);
object.put("preview", previewUrl);
jarr.put(i, object);
}

在 for 循环之后,您可以从键中获取值。

或者,如果 jarr 已经有对象...

    JSONArray jarr = information;

for (int i = 0; i < allData.length(); i++ ) {
final String uploadUrl = allData.getJSONObject(i).getString("content_url");
final String previewUrl = allData.getJSONObject(i).getString("preview_url");

JSONObject object = new jarr.getJSONObject(i);
object.put("content", uploadUrl);
object.put("preview", previewUrl);
jarr.put(i, object);
}

这尚未经过测试,但应该可以工作。哈哈

关于java - JSONObject 未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57977659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com