How to cast JSONArray to int array?

volly

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;
    }
}

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

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

How can I do that?

Client code:

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

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[]).


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:"); 

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

2 other way

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);                                                     

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

It doesn't work anyway..

afsantos

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);
}

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.

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to cast Int to unsigned short in Swift

来自分类Dev

How to make compiler not show int to void pointer cast warnings

来自分类Dev

Cast float to int, or int to float?

来自分类Dev

JSONArray array =新的JSONArray(string_of_json_array);

来自分类Dev

int to char cast,奇怪的行为

来自分类Dev

static_cast int 引用int?

来自分类Dev

IndexedSeq [Int]与Array [Int]

来自分类Dev

如何将JSONArray转换为int数组?

来自分类Dev

具有Int Cast的PDO准备的语句

来自分类Dev

Scala:从Array [Array [Int]]到Array [Int]

来自分类Dev

dynamic_cast(int *到int *)-编译错误

来自分类Dev

对通用枚举集合应用Cast <int> .Cast <int?>会导致无效的强制转换异常

来自分类Dev

std :: bit_cast与std :: array

来自分类Dev

how to cast a variable in xpath python

来自分类Dev

解释static_cast“ static_cast <void(Pet :: *)(int)>”语法吗?

来自分类Dev

flatten a list of int array

来自分类Dev

Why does this function cast an int argument to a volatile pointer and immediately dereferences it?

来自分类Dev

为什么对volatile int的const引用需要static_cast?

来自分类Dev

boost :: lexical_cast <int>(“ -138.8468953457983248”)引发异常

来自分类Dev

带有INT和单个连字符(-)的奇怪CAST行为

来自分类Dev

boost :: lexical_cast <int>(“ -138.8468953457983248”)引发异常

来自分类Dev

带有INT和单个连字符(-)的奇怪CAST行为

来自分类Dev

Firebase iOS -Cast StorageUploadTask 进度从 Double 到 Int

来自分类Dev

如何使用 SQLite 和 cast as int 更新查询

来自分类Dev

android:JSONArray类型的方法getJSONObject(int)不适用于参数(字符串)

来自分类Dev

How do I parse a JSONArray in Java with Json.simple?

来自分类Dev

JSONArray 中的 JSONArray

来自分类Dev

从MutableList <Int>初始化Array <Int>

来自分类Dev

为什么reinterpret_cast无法将int转换为int?

Related 相关文章

  1. 1

    How to cast Int to unsigned short in Swift

  2. 2

    How to make compiler not show int to void pointer cast warnings

  3. 3

    Cast float to int, or int to float?

  4. 4

    JSONArray array =新的JSONArray(string_of_json_array);

  5. 5

    int to char cast,奇怪的行为

  6. 6

    static_cast int 引用int?

  7. 7

    IndexedSeq [Int]与Array [Int]

  8. 8

    如何将JSONArray转换为int数组?

  9. 9

    具有Int Cast的PDO准备的语句

  10. 10

    Scala:从Array [Array [Int]]到Array [Int]

  11. 11

    dynamic_cast(int *到int *)-编译错误

  12. 12

    对通用枚举集合应用Cast <int> .Cast <int?>会导致无效的强制转换异常

  13. 13

    std :: bit_cast与std :: array

  14. 14

    how to cast a variable in xpath python

  15. 15

    解释static_cast“ static_cast <void(Pet :: *)(int)>”语法吗?

  16. 16

    flatten a list of int array

  17. 17

    Why does this function cast an int argument to a volatile pointer and immediately dereferences it?

  18. 18

    为什么对volatile int的const引用需要static_cast?

  19. 19

    boost :: lexical_cast <int>(“ -138.8468953457983248”)引发异常

  20. 20

    带有INT和单个连字符(-)的奇怪CAST行为

  21. 21

    boost :: lexical_cast <int>(“ -138.8468953457983248”)引发异常

  22. 22

    带有INT和单个连字符(-)的奇怪CAST行为

  23. 23

    Firebase iOS -Cast StorageUploadTask 进度从 Double 到 Int

  24. 24

    如何使用 SQLite 和 cast as int 更新查询

  25. 25

    android:JSONArray类型的方法getJSONObject(int)不适用于参数(字符串)

  26. 26

    How do I parse a JSONArray in Java with Json.simple?

  27. 27

    JSONArray 中的 JSONArray

  28. 28

    从MutableList <Int>初始化Array <Int>

  29. 29

    为什么reinterpret_cast无法将int转换为int?

热门标签

归档