开发者

Java JSON serialization of an integer array?

开发者 https://www.devze.com 2023-04-04 04:18 出处:网络
This should be simple. I have an integer array that I want to encode in JSON formatting so I can pass it to a web service:

This should be simple.

I have an integer array that I want to encode in JSON formatting so I can pass it to a web service:

int[] myInts = new int[length-1];
myInts = {5, 6, 7, 1, 1开发者_如何学C2, ..., 78}; 

JSONArray jArray = new JSONArray();
//now what? 

Thanks.


If i where you I'd create a JSONObject first and then create the JSONArray and use

myJsonArray.put(value);

and finally add the jsonarray to the JSONObject. I suppose this would work nice for a small set of numbers however if you want to pass a large set of integers there should be a more elegant way to do it than this.


int[] myInts = {1,2,3,4,5}; 
JSONArray jArray = new JSONArray(myInts);
System.out.println(jArray.toString());
0

精彩评论

暂无评论...
验证码 换一张
取 消