當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。