JSONObject更新不正确

时间:2019-09-17 15:35:55

标签: java android arrays json

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

2 个答案:

答案 0 :(得分:1)

感谢更新。

您要将信息项添加到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);
    }

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

答案 1 :(得分:0)

尝试这样

JSONArray jsonArray;
    try {
//here replace with your data object
        jsonArray = new JSONArray(" [{\"type\":\"video\",\"format\":\"mp4\",\"preview_format\":\"jpg\"}, {\"type\":\"video\",\"format\":\"mp4\",\"preview_format\":\"jpg\"}]");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            jsonObject.put("content", "test value " + i);
        }
        Log.d("Print test",  jsonArray.getJSONObject(0).toString());
        Log.d("Print test",  jsonArray.getJSONObject(1).toString());
    } catch (
            JSONException e) {
        e.printStackTrace();
    }