简体   繁体   English

在JSONObject中发送值列表

[英]sending list of values in JSONObject

public class sample {
    public static void main(String[] args) {
        List<String> sList = new ArrayList<String>();
        JSONObject inputJsonObj = new JSONObject();
        try {


            inputJsonObj.put("ipaddress","10.254.27.12");

            inputJsonObj.put("ipaddress", "10.253.140.116");


            Client client = Client.create();
            WebResource webResource = client
                    .resource("http://10.85.249.29:8080/checkRest/CheckxARC/getVersion");
//             

            ClientResponse response = webResource.type("application/json")
                    .post(ClientResponse.class, inputJsonObj.toString());
            String output = response.getEntity(String.class);

            System.out.println(" op--->"+output);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

I have created a client side code for calling restful webservices. 我已经创建了用于调用静态Web服务的客户端代码。 Previously when I was sending single ipaddress it was working perfectly.Now when i am trying to send more than 1 IPadrresses I am being able to proceed.Can anyone suggest me how can I proceed it? 以前,当我发送单个ipaddress时,它运行良好。现在,当我尝试发送多个IPadrress时,我可以继续进行操作。有人可以建议我如何进行操作吗?

Could you not just add an integer value to the key stored for that value? 您是否可以仅将整数值添加到该值存储的键中? Then at the receiving end just write something to loop through and get all values Example: 然后在接收端编写一些要遍历并获取所有值的示例:

  public class sample {
        public static void main(String[] args) {
            List<String> sList = new ArrayList<String>();
            JSONObject inputJsonObj = new JSONObject();
            try {

                inputJsonObj.put("ipaddress","10.254.27.12");
                inputJsonObj.put("ipaddress1", "10.253.140.116"); // Add one to the name

                Client client = Client.create();
                WebResource webResource = client
                        .resource("http://10.85.249.29:8080/checkRest/CheckxARC/getVersion");            

                ClientResponse response = webResource.type("application/json")
                        .post(ClientResponse.class, inputJsonObj.toString());
                String output = response.getEntity(String.class);

                System.out.println(" op--->"+output);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

Get all IP addresses: 获取所有IP地址:

    Collection values = jObject.values();
    values.forEach( System.out::print );

Edited errors in code. 代码中的已编辑错误。

you could use a JSONArray . 您可以使用JSONArray In it, you will store all your ip adress. 在其中,您将存储所有IP地址。

Then, you add the array to your JSONObject using the put method. 然后,使用put方法将数组添加到JSONObject中。

It would look like: 它看起来像:

JSONArray array = new JSONArray();
array.put("10.254.27.12");
array.put("10.254.27.123");

JSONObject inputJsonObj = new JSONObject();
inputJsonObj.put("ipaddresses",array);
// Using ipaddresses as there are many addresses
// The rest of the code stays the same

Note: sorry, I did not add much time to test it, but the logic and objects are there. 注意:抱歉,我没有花太多时间对其进行测试,但是这里有逻辑和对象。 :) :)

Cheers 干杯

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

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