Exemplo n.º 1
0
 public static Double[] json2DoubleArray(String jsonString) {
   JSONArray jsonArray = JSONArray.fromObject(jsonString);
   Double[] doubleArray = new Double[jsonArray.size()];
   for (int i = 0; i < jsonArray.size(); i++) {
     doubleArray[i] = jsonArray.getDouble(i);
   }
   return doubleArray;
 }
  public void testReadArray() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JsonLibModule());

    JSONArray array =
        mapper.readValue("[null, 13, false, 1.25, \"abc\", {\"a\":13}, [ ] ]", JSONArray.class);
    assertEquals(7, array.size());
    assertTrue(JSONUtils.isNull(array.get(0)));
    assertEquals(13, array.getInt(1));
    assertFalse(array.getBoolean(2));
    assertEquals(Double.valueOf(1.25), array.getDouble(3));
    assertEquals("abc", array.getString(4));
    JSONObject ob = array.getJSONObject(5);
    assertEquals(1, ob.size());
    assertEquals(13, ob.getInt("a"));
    JSONArray array2 = array.getJSONArray(6);
    assertEquals(0, array2.size());
  }