本文整理汇总了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);
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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()));
}
}
}
}