我从来没有用过这个,我也没有测试过,但看着你的代码和文档
JSONObject,并
JSONArray,这是我的建议。
// 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,当值不存在,或者不是一个整数。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)