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


Java JSONArray.opt方法代码示例

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


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

示例1: jsonArrayToArray

import org.json.JSONArray; //导入方法依赖的package包/类
/**
 * Converts the specified {@link JSONArray JSON array} to an array.
 *
 * @param <T>
 *            the type of elements maintained by the specified json array
 * @param jsonArray
 *            the specified json array
 * @param newType
 *            the class of the copy to be returned
 * @return an array
 */
@SuppressWarnings("unchecked")
public static <T> T[] jsonArrayToArray(final JSONArray jsonArray, final Class<? extends T[]> newType) {
	if (null == jsonArray) {
		return (T[]) new Object[] {};
	}

	final int newLength = jsonArray.length();
	final Object[] original = new Object[newLength];

	for (int i = 0; i < newLength; i++) {
		original[i] = jsonArray.opt(i);
	}

	return Arrays.copyOf(original, newLength, newType);
}
 
开发者ID:daima,项目名称:solo-spring,代码行数:27,代码来源:CollectionUtils.java

示例2: listFromJson

import org.json.JSONArray; //导入方法依赖的package包/类
public static <T> List<T> listFromJson(JSONArray json) {
  if (json == null) {
    return null;
  }
  List<Object> result = new ArrayList<>(json.length());
  for (int i = 0; i < json.length(); i++) {
    Object value = json.opt(i);
    if (value == null || value == JSONObject.NULL) {
      value = null;
    } else if (value instanceof JSONObject) {
      value = mapFromJson((JSONObject) value);
    } else if (value instanceof JSONArray) {
      value = listFromJson((JSONArray) value);
    } else if (JSONObject.NULL.equals(value)) {
      value = null;
    }
    result.add(value);
  }
  return CollectionUtil.uncheckedCast(result);
}
 
开发者ID:Leanplum,项目名称:Leanplum-Android-SDK,代码行数:21,代码来源:JsonConverter.java

示例3: parseJSONArray

import org.json.JSONArray; //导入方法依赖的package包/类
/**
 * 解析JSONArray 获取数组 例如:["1",2,true,"4"]
 *
 * @param jsonArray 数据源
 * @param array     返回值
 * @return
 */
public static Object[] parseJSONArray(JSONArray jsonArray, Object[] array) {
    if (jsonArray != null) {
        try {
            array = new Object[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                array[i] = jsonArray.opt(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return array;
}
 
开发者ID:quickhybrid,项目名称:quickhybrid-android,代码行数:21,代码来源:JsonUtil.java

示例4: vertexAtIndex

import org.json.JSONArray; //导入方法依赖的package包/类
private static PointF vertexAtIndex(int idx, JSONArray points) {
  if (idx >= points.length()) {
    throw new IllegalArgumentException(
        "Invalid index " + idx + ". There are only " + points.length() + " points.");
  }

  JSONArray pointArray = points.optJSONArray(idx);
  Object x = pointArray.opt(0);
  Object y = pointArray.opt(1);
  return new PointF(
      x instanceof Double ? new Float((Double) x) : (int) x,
      y instanceof Double ? new Float((Double) y) : (int) y);
}
 
开发者ID:alibaba,项目名称:atlas,代码行数:14,代码来源:ShapeData.java

示例5: fromJson

import org.json.JSONArray; //导入方法依赖的package包/类
private ArrayList<Object> fromJson(JSONArray jSONArray) throws JSONException {
    ArrayList<Object> arrayList = new ArrayList();
    int length = jSONArray.length();
    for (int i = 0; i < length; i++) {
        Object opt = jSONArray.opt(i);
        if (opt instanceof JSONObject) {
            opt = fromJson((JSONObject) opt);
        } else if (opt instanceof JSONArray) {
            opt = fromJson((JSONArray) opt);
        }
        arrayList.add(opt);
    }
    return arrayList;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:15,代码来源:Hashon.java

示例6: parseFromDB

import org.json.JSONArray; //导入方法依赖的package包/类
public static MixMessage parseFromDB(MessageEntity entity) throws JSONException {
    if(entity.getDisplayType() != DBConstant.SHOW_MIX_TEXT){
        throw new RuntimeException("#MixMessage# parseFromDB,not SHOW_MIX_TEXT");
    }
    Gson gson = new GsonBuilder().create();
    MixMessage mixMessage = new MixMessage(entity);
    List<MessageEntity> msgList = new ArrayList<>();
    JSONArray jsonArray = new JSONArray(entity.getContent());

    for (int i = 0, length = jsonArray.length(); i < length; i++) {
        JSONObject jsonOb = (JSONObject) jsonArray.opt(i);
        int displayType = jsonOb.getInt("displayType");
        String jsonMessage = jsonOb.toString();
        switch (displayType){
            case DBConstant.SHOW_ORIGIN_TEXT_TYPE:{
                TextMessage textMessage = gson.fromJson(jsonMessage,TextMessage.class);
                textMessage.setSessionKey(entity.getSessionKey());
                msgList.add(textMessage);
            }break;

            case DBConstant.SHOW_IMAGE_TYPE:
                ImageMessage imageMessage = gson.fromJson(jsonMessage,ImageMessage.class);
                imageMessage.setSessionKey(entity.getSessionKey());
                msgList.add(imageMessage);
                break;
        }
    }
    mixMessage.setMsgList(msgList);
    return mixMessage;
}
 
开发者ID:ccfish86,项目名称:sctalk,代码行数:31,代码来源:MixMessage.java

示例7: Response

import org.json.JSONArray; //导入方法依赖的package包/类
public Response(JSONObject json) {
    // status defaults to success
    mStatus = new Status();
    JSONObject error = json.optJSONObject(Keys.Error);
    if (error != null) {
        mStatus.setSuccess(false);
        mStatus.setErrorMessage(error.optString(Keys.Message, "Unknown Error"));
        mStatus.setStatusCode(error.optInt(Keys.Status, 500));
    }

    // top-level JSON properties
    mConversationId = json.optString(Keys.ConversationId);
    mParticipantId = json.optString(Keys.ParticipantId);
    mETag = json.optString(Keys.ETag);
    mAsrHypothesis = json.optString(Keys.AsrHypothesis);
    mEndpoint = json.optString(Keys.EndpointHeader);
    mTimedResponseInterval = json.optDouble(Keys.TimedResponseInterval, 0);

    // last modified
    String interval = json.optString(Keys.LastModified);
    if (!interval.isEmpty()) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
        try {
            mLastModified = dateFormat.parse(interval);
        } catch (ParseException e) {
            e.printStackTrace();
            mLastModified = null;
        }
    }

    // outputs
    mOutputs = new ArrayList<>();
    JSONArray outputs = json.optJSONArray(Keys.Outputs);
    if (outputs != null) {
        for (int i = 0; i < outputs.length(); i++) {
            JSONObject output = outputs.optJSONObject(i);
            if (output == null || !output.has(Keys.OutputType)) {
                continue;
            }

            String type = output.optString(Keys.OutputType);

            if (type.equals(OutputType.BEHAVIOR.toString())) {
                mOutputs.add(new BehaviorOutput(output));
            } else if (type.equals(OutputType.DIALOG.toString())) {
                mOutputs.add(new DialogOutput(output));
            }
        }
    }

    // entities -- convert from JSON dictionary to an array
    mEntities = new ArrayList<>();
    JSONObject entities = json.optJSONObject(Keys.Entities);
    if (entities != null) {
        Iterator<String> keys = entities.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            Object entity = entities.opt(key);

            if (entity == null) {
                continue;
            }

            if (entity instanceof String) {
                mEntities.add(new Label(key, (String)entity));
            } else if (entity instanceof JSONArray) {
                JSONArray arr = (JSONArray) entity;
                ArrayList<Object> list = new ArrayList<>();
                for (int i = 0; i < arr.length(); i++) {
                    Object item = arr.opt(i);
                    if (item != null) list.add(item);
                }
                mEntities.add(new List(key, list));
            } else if (entity instanceof Boolean) {
                mEntities.add(new Flag(key, (boolean)entity));
            } else if (entity instanceof Number) {
                mEntities.add(new Counter(key, ((Number)entity).doubleValue()));
            }
        }
    }
}
 
开发者ID:pullstring,项目名称:pullstring-android,代码行数:82,代码来源:Response.java


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