当前位置: 首页>>代码示例>>Java>>正文


Java JSONArray.set方法代码示例

本文整理汇总了Java中com.alibaba.fastjson.JSONArray.set方法的典型用法代码示例。如果您正苦于以下问题:Java JSONArray.set方法的具体用法?Java JSONArray.set怎么用?Java JSONArray.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.alibaba.fastjson.JSONArray的用法示例。


在下文中一共展示了JSONArray.set方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: test_0

import com.alibaba.fastjson.JSONArray; //导入方法依赖的package包/类
public void test_0() throws Exception {
    JSONArray array = new JSONArray();
    array.set(1, "1001");
    Assert.assertEquals(2, array.size());
    Assert.assertNull(array.get(0));
    Assert.assertEquals("1001", array.get(1));
    
    array.clear();
    Assert.assertEquals(0, array.size());
    
    array.set(-1, "1001");
    Assert.assertEquals(1, array.size());
    Assert.assertEquals("1001", array.get(0));
    
    array.fluentAdd("1002").fluentClear();
    Assert.assertEquals(0, array.size());
    
    array.fluentAdd("1002").fluentRemove("1002");
    Assert.assertEquals(0, array.size());
    
    array.fluentAdd("1002").fluentRemove(0);
    Assert.assertEquals(0, array.size());
    
    array.fluentSet(1, "1001");
    Assert.assertEquals(2, array.size());
    Assert.assertNull(array.get(0));
    Assert.assertEquals("1001", array.get(1));

    array.fluentRemoveAll(Arrays.asList(null, "1001"));
    Assert.assertEquals(0, array.size());
    
    array.fluentAddAll(Arrays.asList("1001", "1002", "1003"));
    Assert.assertEquals(3, array.size());
    
    array.retainAll(Arrays.asList("1002", "1004"));
    Assert.assertEquals(1, array.size());
    Assert.assertEquals("1002", array.get(0));
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:39,代码来源:JSONArrayTest3.java

示例2: mergeJSONArray

import com.alibaba.fastjson.JSONArray; //导入方法依赖的package包/类
private static void mergeJSONArray(JSONArray target, JSONArray from, ArrayMergeStrategy strategy){
    switch (strategy) {
        case REPLACE:
            if (target.size() > from.size()) {
                for (int i = 0; i < from.size(); i++) {
                    target.set(i, from.get(i));
                }
            } else {
                target.clear();
                target.addAll(from);
            }
            break;
        case APPEND:
            target.addAll(from);
            break;
        case DEEP_MERGE:
            // 对位置相同的元素进行合并;
            int mergeCount = Math.min(target.size(), from.size());
            Object targetFieldValue;
            Object fromFieldValue;
            for (int i = 0; i < mergeCount; i++) {
                targetFieldValue = target.get(i);
                fromFieldValue = from.get(i);
                if ((targetFieldValue instanceof JSONObject) && (fromFieldValue instanceof JSONObject)) {
                    // 都是 JSON 对象,进行深度合并;
                    mergeJSONObject((JSONObject) targetFieldValue, (JSONObject) fromFieldValue, strategy);
                } else if ((targetFieldValue instanceof JSONArray) && (fromFieldValue instanceof JSONArray)) {
                    // 都是 JSON 数组,进行数组合并;
                    mergeJSONArray((JSONArray) targetFieldValue, (JSONArray) fromFieldValue, strategy);
                } else {
                    // 类型不同,直接替换;
                    target.set(i, fromFieldValue);
                }
            }
            // 对多出来的元素进行追加;
            if (from.size() > target.size()) {
                for (int i = target.size(); i < from.size(); i++) {
                    target.add(from.get(i));
                }
            }
            break;
        default:
            throw new IllegalStateException("Unsupported strategy[" + strategy + "] for merging json array!");
    }
}
 
开发者ID:bubicn,项目名称:bubichain-sdk-java,代码行数:46,代码来源:JSONBean.java

示例3: toArray

import com.alibaba.fastjson.JSONArray; //导入方法依赖的package包/类
/**
 * array.set(index, isContainer ? value : value.getJSONObject(className));
 * @param arrayObject
 * @param className className.isEmpty() == false && value.containsKey(className) >> isContainer = true;
 * @return
 */
public static JSONArray toArray(JSONObject arrayObject, String className) {
	Set<String> set = arrayObject == null ? null : arrayObject.keySet();
	if (set == null || set.isEmpty()) {
		return null;
	}

	// [{...},{...},...]
	String parentString = StringUtil.getTrimedString(JSON.toJSONString(arrayObject));
	if (parentString.isEmpty()) {
		return null;
	}
	if (parentString.startsWith("[")) {
		if (parentString.endsWith("]") == false) {
			parentString += "]";
		}
		return JSON.parseArray(parentString);
	}

	//{"0":{Table:{...}}, "1":{Table:{...}}...}

	className = StringUtil.getTrimedString(className);
	boolean isContainer = true;

	JSONArray array = new JSONArray(set.size());
	JSONObject value;
	boolean isFirst = true;
	int index;
	for (String key : set) {//0, 1, 2,...
		value = StringUtil.isNumer(key) == false ? null : arrayObject.getJSONObject(key);// Table:{}
		if (value != null) {
			try {
				index = Integer.valueOf(0 + key);
				if (isFirst && className.isEmpty() == false && value.containsKey(className)) {// 判断是否需要提取table
					isContainer = false;
				}
				array.set(index, isContainer ? value : value.getJSONObject(className));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		isFirst = false;
	}
	return array;
}
 
开发者ID:TommyLemon,项目名称:APIJSON-Android-RxJava,代码行数:51,代码来源:JSONResponse.java

示例4: toObjectArray

import com.alibaba.fastjson.JSONArray; //导入方法依赖的package包/类
private <T> T toObjectArray(DefaultJSONParser parser, Class<?> componentType, JSONArray array) {
    if (array == null) {
        return null;
    }
    int size = array.size();
    T objArray = Array.newInstance(componentType, size);
    for (int i = 0; i < size; i++) {
        Object value = array.get(i);
        if (value == array) {
            Array.set(objArray, i, objArray);
        } else if (componentType.isArray()) {
            if (componentType.isInstance(value)) {
                element = value;
            } else {
                element = toObjectArray(parser, componentType, (JSONArray) value);
            }
            Array.set(objArray, i, element);
        } else {
            element = null;
            if (value instanceof JSONArray) {
                boolean contains = false;
                JSONArray valueArray = (JSONArray) value;
                int valueArraySize = valueArray.size();
                for (int y = 0; y < valueArraySize; y++) {
                    if (valueArray.get(y) == array) {
                        valueArray.set(i, objArray);
                        contains = true;
                    }
                }
                if (contains) {
                    element = valueArray.toArray();
                }
            }
            if (element == null) {
                element = TypeUtils.cast(value, (Class) componentType, parser.getConfig());
            }
            Array.set(objArray, i, element);
        }
    }
    array.setRelatedArray(objArray);
    array.setComponentType(componentType);
    return objArray;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:44,代码来源:ArrayDeserializer.java


注:本文中的com.alibaba.fastjson.JSONArray.set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。