如何将JSONArray转换为int数组? [英] How to cast JSONArray to int array?

查看:171
本文介绍了如何将JSONArray转换为int数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用方法JSONObject sayJSONHello()时遇到问题.

I'm having problems with the method JSONObject sayJSONHello().

@Path("/hello")
public class SimplyHello {

    @GET
    @Produces(MediaType.APPLICATION_JSON)

     public JSONObject sayJSONHello() {      

        JSONArray numbers = new JSONArray();

        numbers.put(1);
        numbers.put(2);
        numbers.put(3);
        numbers.put(4);             

        JSONObject result = new JSONObject();

        try {
            result.put("numbers", numbers);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }
}

在客户端,我想获取一个int数组[1, 2, 3, 4]而不是JSON

In the client side, I want to get an int array, [1, 2, 3, 4], instead of the JSON

{"numbers":[1,2,3,4]}

我该怎么做?

客户代码:

System.out.println(service.path("rest").path("hello")
    .accept(MediaType.APPLICATION_JSON).get(String.class));

我的方法返回一个JSONObject,但是我想从中提取数字,以便使用这些数字进行计算(例如,作为int[]).

My method returns a JSONObject, but I want to extract the numbers from it, in order to perform calculations with these (e.g as an int[]).

我公开了作为JSONObject的功能.

I reveive function as a JSONObject.

 String y = service.path("rest").path("hello").accept(MediaType.APPLICATION_JSON).get(String.class);   
JSONObject jobj = new JSONObject(y);   
int [] id = new int[50];
 id = (int [] ) jobj.optJSONObject("numbers:"); 

然后我得到错误:无法从JSONObject转换为int []

And then i get error: Cannot cast from JSONObject to int[]

2种其他方式

String y = service.path("rest").path("hello").accept(MediaType.APPLICATION_JSON).get(String.class);   
JSONArray obj = new JSONArray(y);  
int [] id = new int[50];      
 id = (int [] ) obj.optJSONArray(0);                                                     

这次,我得到:无法从JSONArray转换为int [] ...

And this time i get: Cannot cast from JSONArray to int[]...

反正不行.

推荐答案

我从没有使用过它,也没有测试过它,但是请查看您的代码和 JSONArray ,这就是我的建议.

I've never used this, nor have I tested it, but looking at your code and the documentation for JSONObject and JSONArray, this is what I suggest.

// Receive JSON from server and parse it.
String jsonString = service.path("rest").path("hello")
    .accept(MediaType.APPLICATION_JSON).get(String.class);
JSONObject obj = new JSONObject(jsonString);

// Retrieve number array from JSON object.
JSONArray array = obj.optJSONArray("numbers");

// Deal with the case of a non-array value.
if (array == null) { /*...*/ }

// Create an int array to accomodate the numbers.
int[] numbers = new int[array.length()];

// Extract numbers from JSON array.
for (int i = 0; i < array.length(); ++i) {
    numbers[i] = array.optInt(i);
}

这应该适合您的情况.在更严重的应用程序上,您可能需要检查值是否确实是整数,因为当值不存在或不是整数时,optInt返回0.

This should work for your case. On a more serious application, you may want to check if the values are indeed integers, as optInt returns 0 when the value does not exist, or isn't an integer.

获取与索引关联的可选int值.如果索引没有值,或者该值不是数字并且不能转换为数字,则返回零.

Get the optional int value associated with an index. Zero is returned if there is no value for the index, or if the value is not a number and cannot be converted to a number.

这篇关于如何将JSONArray转换为int数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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