如何在java 8中迭代JSONArray [英] How to iterate over a JSONArray in java 8

查看:108
本文介绍了如何在java 8中迭代JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,它使用 for循环来迭代 JSONArray 中的元素。

I have the following code which is using a for loop to iterate over the elements in a JSONArray.

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.stream.IntStream;
    public class Pmt {
    private String[] patchInformation_svnRevisionpublic;
    private final Logger logger = Logger.getLogger(Pmt.class.getName());

        private static final String COMMITS_IN_PATCH_IDENTIFIER = "patchInformation_svnRevisionpublic";  //key used to identify the commits in a patch from JSON response received from PMT
        private static final String KEY_STRING = "name";
        private static final String VALUE_STRING = "value";

         public String[] getPublicGitCommitHashes(JSONArray jsonArray) {

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                String tempName = (String) jsonObject.get(KEY_STRING);
                if (tempName.equals(COMMITS_IN_PATCH_IDENTIFIER)) {
                    JSONArray tempCommitsJSONArray = (JSONArray) jsonObject.get(VALUE_STRING);
                    //initializing the patchInformation_svnRevisionpublic array
                    patchInformation_svnRevisionpublic = new String[tempCommitsJSONArray.length()];
                    // for ommiting the white spaces at the begingin and end of the commits
                    IntStream.range(0, tempCommitsJSONArray.length()).forEach(j -> patchInformation_svnRevisionpublic[j] = ((String) tempCommitsJSONArray.get(j)).trim());

                    logger.info(" The commits hashes obtained from WSO2 PMT are successfully saved to an array");

                    System.out.println("The commit Ids are");
                    //            for printing all the commits ID associated with a patch
                    IntStream.range(0, patchInformation_svnRevisionpublic.length).mapToObj(i1 -> patchInformation_svnRevisionpublic[i1]).forEach(System.out::println);
                    System.out.println();
                    break;
                }
            }
            //to prevent from internaal representation by returning referecnce to mutable object
            String clonedPatchInformation_svnRevisionpublic[] = patchInformation_svnRevisionpublic.clone();
            return clonedPatchInformation_svnRevisionpublic;
        }
    }

如何使用<$ c中的新功能$ c> Java 8 如 streams API forEach 执行相同的任务。在此先感谢

How do I use the new features in Java 8 like streams API or forEach to perform the same task. Thanks in advance

推荐答案

这相当于Java 8流API中的代码或代码。不是100%等价,但你可以得到主要想法。

This is equivalent of or code in Java 8 stream API. Not 100% equivalent, but you can get the main idea.

private static final String COMMITS_IN_PATCH_IDENTIFIER = "patchInformation_svnRevisionpublic";  //key used to identify the commits in a patch from JSON response received from PMT
private static final String KEY_STRING = "name";
private static final String VALUE_STRING = "value";

public List<String> getCommitIds (JSONArray array) {
     return arrayToStream(array)
            .map(JSONObject.class::cast)
            .filter(o -> o.get(KEY_STRING).equals(COMMITS_IN_PATCH_IDENTIFIER))
            .findFirst()
            .map(o -> (JSONArray) o.get(VALUE_STRING))
            .map(Main::arrayToStream)
            .map(commits ->
                    commits.map(Object::toString)
                            .map(String::trim)
                            .collect(Collectors.toList())
            )
            .orElseGet(Collections::emptyList);
}

@Nonnull
private static Stream<Object> arrayToStream(JSONArray array) {
    return StreamSupport.stream(array.spliterator(), false);
}

这篇关于如何在java 8中迭代JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆