简体   繁体   English

如何访问jsonarray elemennts

[英]how to access jsonarray elemennts

 @Path("/getVersion")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String getVersion(String getVersionJson) {
        String version = "", patches = "", connectionStatus = "", output1 = "", output2 = "";

        try {

            JSONObject inputJson = new JSONObject(getVersionJson);

            String ip = inputJson.getString("ipaddress").trim();

            String userName = inputJson.getString("username").trim();
            String passWord = inputJson.getString("password").trim();
            connectionStatus = getSSHConnection(ip, userName, passWord);

            if (connectionStatus.equals("Connected")) {

                //Version Check
                expect.send("bwshowver" + "\n");
                if (expect.expect("$") > -1) {
                    String contt = "";
                    contt = (expect.before);
                    if (contt != null && contt != "") {

                        contt = contt.replaceAll("\n+", "\n");
                        contt = contt.replaceAll(" +", " ");

                        String splitter[] = contt.split("\n");

                        for (int i = 0; i < splitter.length; i++) {
//                         
                            if (splitter[i].contains("Patches")) {
                                patches = splitter[i];
                            }
                            //version
                            if (splitter[i].contains("version")) {
                                version = splitter[i];
                            }
//                            output1=version.toString();
//                            output2=patches.toString();
//                             output3=output1+output2;
//                                     
                            output1 = contt;
                        }

                    }

                } else {
                    output1 = "Error in version check";
                    System.out.println("Error in version check");
                }

            } else {
                output1 = connectionStatus;
                System.out.println(connectionStatus);
            }
        } catch (Exception e) {
            output1 = "Error";
            //     logger.error("Exception in getVersion Function-ServService Class: " + e.getMessage());
        } finally {
            stopSSH();
        }

        return output3;
    }

//The string which is being passed from getVersion comprises of //从getVersion传递的字符串包含

[{"ipaddress":"10.253.140.116","password":"c0mcast!","username":"bwadmin"},{"ipaddress":"10.253.140.117","password":"bwadmin!","username":"bwadmin"}]

//My requirement is to access the value of ipaddress and password and username and store items in an array and send them to ConnectionStatus. //我的要求是访问ipaddress的值和密码以及用户名并将项目存储在数组中并将它们发送到ConnectionStatus。

JSONArray jsonArr = new JSONArray(getVersionJson);

for (int i = 0; i < jsonArr.length(); i++) {
    JSONObject jsonObj = jsonArr.getJSONObject(i);
    //Now you can fetch values from each JSON Object
}

JSONObject class represents just one JSON. JSONObject类只代表一个JSON。 This is generally inside braces { and } . 这通常在括号内{}
If the expected input getVersionJson is just one JSON object, your code works. 如果预期的输入getVersionJson只是一个JSON对象,那么您的代码就可以运行。

However, you are getting an array of JSON objects in the input string getVersionJson , so you need to use JSONArray class to handle an array of JSON Objects. 但是,您在输入字符串getVersionJson中获取了一组JSON对象,因此您需要使用JSONArray类来处理JSON对象数组。
This is generally [ { }, { }, { }, ... ] 这通常是[ { }, { }, { }, ... ]

JSONArray extends List , so it can be iterated to read each JSONObject value. JSONArray扩展了List ,因此可以迭代它来读取每个JSONObject值。

Here is documentation for reference: Interface JsonArray . 以下是参考文档: Interface JsonArray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM