Package com.alibaba.fastjson

Examples of com.alibaba.fastjson.JSONArray


                lexer.nextToken();
                TreeSet<Object> treeSet = new TreeSet<Object>();
                parseArray(treeSet, fieldName);
                return treeSet;
            case LBRACKET:
                JSONArray array = new JSONArray();
                parseArray(array, fieldName);
                return array;
            case LBRACE:
                JSONObject object = new JSONObject();
                return parseObject(object, fieldName);
View Full Code Here


            text = JSON.toJSONString(list);
           
            System.out.println(text);
        }

        JSONArray array = JSON.parseArray(text);

        Body body = array.getObject(1, Body.class);

        Assert.assertEquals("张三", body.getName());
        Assert.assertEquals(1, body.getItems().size());
    }
View Full Code Here

public class JSONArrayTest extends TestCase {

    public void test_toString() throws Exception {
        StringWriter out = new StringWriter();
        new JSONArray().writeJSONString(out);
        Assert.assertEquals("[]", out.toString());
        Assert.assertEquals("[]", new JSONArray().toString());
    }
View Full Code Here

        Assert.assertEquals("null", JSONArray.toJSONString(null));
        Assert.assertEquals("[null]", JSONArray.toJSONString(Collections.singletonList(null)));
    }

    public void test_1() throws Exception {
        JSONArray array = new JSONArray(3);
        Assert.assertEquals(true, array.isEmpty());
        array.add(1);
        Assert.assertEquals(false, array.isEmpty());
        Assert.assertEquals(true, array.contains(1));
        Assert.assertEquals(1, array.toArray()[0]);
        {
            Object[] items = new Object[1];
            array.toArray(items);
            Assert.assertEquals(1, items[0]);
        }
        Assert.assertEquals(true, array.containsAll(Collections.singletonList(1)));
        Assert.assertEquals(true, array.remove(Integer.valueOf(1)));
        Assert.assertEquals(true, array.isEmpty());
        array.addAll(Collections.singletonList(1));
        Assert.assertEquals(1, array.size());
        array.removeAll(Collections.singletonList(1));
        Assert.assertEquals(0, array.size());
        array.addAll(0, Arrays.asList(1, 2, 3));
        Assert.assertEquals(3, array.size());
        array.clear();
        array.addAll(0, Arrays.asList(1, 2, 3));
        Assert.assertEquals(true, array.retainAll(Arrays.asList(1, 2)));
        Assert.assertEquals(2, array.size());
        Assert.assertEquals(true, array.retainAll(Arrays.asList(2, 4)));
        Assert.assertEquals(1, array.size());
        array.set(0, 4);
        Assert.assertEquals(4, array.toArray()[0]);
        array.add(0, 4);
        Assert.assertEquals(4, array.toArray()[0]);
        array.remove(0);
        array.remove(0);
        Assert.assertEquals(0, array.size());
        array.addAll(Arrays.asList(1, 2, 3, 4, 5, 4, 3));
        Assert.assertEquals(2, array.indexOf(3));
        Assert.assertEquals(6, array.lastIndexOf(3));
        {
            AtomicInteger count = new AtomicInteger();
            for (ListIterator<Object> iter = array.listIterator(); iter.hasNext(); iter.next()) {
                count.incrementAndGet();
            }
            Assert.assertEquals(7, count.get());
        }
        {
            AtomicInteger count = new AtomicInteger();
            for (ListIterator<Object> iter = array.listIterator(2); iter.hasNext(); iter.next()) {
                count.incrementAndGet();
            }
            Assert.assertEquals(5, count.get());
        }
        {
            Assert.assertEquals(2, array.subList(2, 4).size());
        }
    }
View Full Code Here

    public void test_array() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("[[],[],3,null,{\"name\":\"jobs\"},{\"id\":123},{\"id\":1},{\"id\":2}]"));
        reader.startArray();

        JSONArray first = (JSONArray) reader.readObject();
        JSONArray second = (JSONArray) reader.readObject();

        Assert.assertNotNull(first);
        Assert.assertNotNull(second);

        Assert.assertEquals(new Integer(3), reader.readInteger());
View Full Code Here

import com.alibaba.fastjson.JSONArray;

public class JSONArrayTest_hashCode extends TestCase {

    public void test_hashCode() throws Exception {
        Assert.assertEquals(new JSONArray().hashCode(), new JSONArray().hashCode());
    }
View Full Code Here

        JSONObject json = JSON.parseObject("{\"val\":12.3}");
        Assert.assertTrue(12.3D == json.getDoubleValue("val"));
    }

    public void test_JSONArray_double() throws Exception {
        JSONArray json = JSON.parseArray("[12.3]");
        Assert.assertTrue(12.3D == json.getDoubleValue(0));
    }
View Full Code Here

    content = content.trim();
    Assert.assertTrue(content.startsWith("fetchAppList("));
    Assert.assertTrue(content.endsWith(");"));
    String json = content.replaceFirst("fetchAppList\\(", "");
    json = json.replaceFirst("\\);", "");
    JSONArray data = (JSONArray)JSON.parse(json);
    Assert.assertEquals(1, data.size());
    @SuppressWarnings("unchecked")
    Map<String, Object> map = (Map<String, Object>)data.get(0);
    Assert.assertEquals("1000", map.get("appId"));
   
  }
View Full Code Here

                    }

                    object.put(key, value);
                } else if (ch == '[') { // 减少潜套,兼容android
                    lexer.nextToken();
                    JSONArray list = new JSONArray();
                    this.parseArray(list);
                    value = list;
                    object.put(key, value);

                    if (lexer.token() == JSONToken.RBRACE) {
View Full Code Here

                    case LBRACE:
                        JSONObject object = new JSONObject();
                        value = parseObject(object, i);
                        break;
                    case LBRACKET:
                        Collection items = new JSONArray();
                        parseArray(items, i);
                        value = items;
                        break;
                    case NULL:
                        value = null;
View Full Code Here

TOP

Related Classes of com.alibaba.fastjson.JSONArray

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.