JSONObject 未正确更新

我正在更新 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 调用接收到的信息,并且与信息具有相同的长度


胡子哥哥
浏览 143回答 2
2回答

慕无忌1623718

您想要将信息项添加到 JSONObject 中,然后将该对象添加到 JSONArray 中。&nbsp; &nbsp; JSONArray jarr = information;&nbsp; &nbsp; for (int i = 0; i < allData.length(); i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; final String uploadUrl = allData.getJSONObject(i).getString("content_url");&nbsp; &nbsp; &nbsp; &nbsp; final String previewUrl = allData.getJSONObject(i).getString("preview_url");&nbsp; &nbsp; &nbsp; &nbsp; JSONObject object = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; object.put("content", uploadUrl);&nbsp; &nbsp; &nbsp; &nbsp; object.put("preview", previewUrl);&nbsp; &nbsp; &nbsp; &nbsp; jarr.put(i, object);&nbsp; &nbsp; }在 for 循环之后,您可以从键中获取值。或者,如果 jarr 已经有对象......&nbsp; &nbsp; JSONArray jarr = information;&nbsp; &nbsp; for (int i = 0; i < allData.length(); i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; final String uploadUrl = allData.getJSONObject(i).getString("content_url");&nbsp; &nbsp; &nbsp; &nbsp; final String previewUrl = allData.getJSONObject(i).getString("preview_url");&nbsp; &nbsp; &nbsp; &nbsp; JSONObject object = new jarr.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; object.put("content", uploadUrl);&nbsp; &nbsp; &nbsp; &nbsp; object.put("preview", previewUrl);&nbsp; &nbsp; &nbsp; &nbsp; jarr.put(i, object);&nbsp; &nbsp; }这没有经过测试,但应该可以工作。哈哈

12345678_0001

尝试这样,JSONArray jsonArray;&nbsp; &nbsp; try {//here replace with your data object&nbsp; &nbsp; &nbsp; &nbsp; jsonArray = new JSONArray(" [{\"type\":\"video\",\"format\":\"mp4\",\"preview_format\":\"jpg\"}, {\"type\":\"video\",\"format\":\"mp4\",\"preview_format\":\"jpg\"}]");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObject.put("content", "test value " + i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Log.d("Print test",&nbsp; jsonArray.getJSONObject(0).toString());&nbsp; &nbsp; &nbsp; &nbsp; Log.d("Print test",&nbsp; jsonArray.getJSONObject(1).toString());&nbsp; &nbsp; } catch (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java