本文整理汇总了Java中com.google.gson.JsonObject.getAsJsonPrimitive方法的典型用法代码示例。如果您正苦于以下问题:Java JsonObject.getAsJsonPrimitive方法的具体用法?Java JsonObject.getAsJsonPrimitive怎么用?Java JsonObject.getAsJsonPrimitive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.JsonObject
的用法示例。
在下文中一共展示了JsonObject.getAsJsonPrimitive方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: update
import com.google.gson.JsonObject; //导入方法依赖的package包/类
private void update() {
try {
URLConnection conn = new URL(url).openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
String json = "";
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
json = reader.lines().collect(Collectors.joining("\n"));
}
json = json.trim().replaceAll("\\[", "").replaceAll("\\]", ""); //quick and dirty fix
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
JsonPrimitive priceDollars = jsonObject.getAsJsonPrimitive("price_usd");
price = priceDollars.getAsString();
int maxIndex = price.indexOf(".") + 3;
if (price.length() >= maxIndex)
price = price.substring(0, maxIndex);
} catch (Exception e) {
e.printStackTrace();
price = "0.00";
}
price = "{\"price\":\"" + price + "\"}";
}
示例2: Parameter
import com.google.gson.JsonObject; //导入方法依赖的package包/类
public Parameter(JsonObject param) {
try {
setNameAndType(param.get("name").getAsString(), param.get("type").getAsString());
if(param.get("value").isJsonObject()){
setJsonContent(param.getAsJsonObject("value"));
} else {
JsonPrimitive value = param.getAsJsonPrimitive("value");
if(value.isString()) {
setStringContent(value.getAsString());
} else if(value.isNumber()) {
setDoubleContent(value.getAsDouble());
} else if(value.isBoolean()) {
setBooleanContent(value.getAsBoolean());
}
}
} catch (Exception e){
throw new IllegalArgumentException("invalid parameter structure");
}
}
示例3: decryptCardCode
import com.google.gson.JsonObject; //导入方法依赖的package包/类
/**
* 卡券Code解码
*
* @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得
* @return 解密后的Code
*/
@Override
public String decryptCardCode(String encryptCode) throws WxErrorException {
String url = "https://api.weixin.qq.com/card/code/decrypt";
JsonObject param = new JsonObject();
param.addProperty("encrypt_code", encryptCode);
String responseContent = this.wxMpService.post(url, param.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code");
return jsonPrimitive.getAsString();
}
示例4: parse
import com.google.gson.JsonObject; //导入方法依赖的package包/类
private void parse(final StringBuilder buffer, final String message) {
final JsonParser parser = new JsonParser();
try {
final JsonElement element = parser.parse(new StringReader(message));
if(element.isJsonObject()) {
final JsonObject json = element.getAsJsonObject();
final JsonObject error = json.getAsJsonObject("error");
if(null == error) {
this.append(buffer, message);
}
else {
final JsonPrimitive tag = error.getAsJsonPrimitive(".tag");
if(null == tag) {
this.append(buffer, message);
}
else {
this.append(buffer, StringUtils.replace(tag.getAsString(), "_", " "));
}
}
}
if(element.isJsonPrimitive()) {
this.append(buffer, element.getAsString());
}
}
catch(JsonParseException e) {
// Ignore
}
}
示例5: execute
import com.google.gson.JsonObject; //导入方法依赖的package包/类
public void execute() throws IOException, URISyntaxException, AuthenticationException {
JsonObject responseJson = new JsonObject();
WorkItemMover mover = new WorkItemMover(parentService);
boolean isMoved = false;
boolean previewOnly = false;
Collection<AttributeDefinition> moveResults = null;
// read request data
JsonObject workItemData = RequestReader.readAsJson(request);
JsonPrimitive previewPrimitive = workItemData.getAsJsonPrimitive("previewOnly");
JsonPrimitive targetPA = workItemData.getAsJsonPrimitive("targetProjectArea");
JsonArray workItemJson = workItemData.getAsJsonArray("workItems");
JsonArray typeMappingJson = workItemData.getAsJsonArray("typeMapping");
JsonArray attributesJson = workItemData.getAsJsonArray("mapping");
if(previewPrimitive != null) {
previewOnly = previewPrimitive.getAsBoolean();
}
// map cient data to model
Collection<Integer> clientWorkItemList = gson.fromJson(workItemJson, workItemIdCollectionType);
Collection<AttributeDefinition> clientMappingDefinitions = gson.fromJson(attributesJson, attributesCollectionType);
Collection<TypeMappingEntry> typeMappingDefinitions = gson.fromJson(typeMappingJson, typeMappingCollectionType);
Map<String, String> typeMap = new HashMap<String, String>();
for (TypeMappingEntry def : typeMappingDefinitions) {
typeMap.put(def.getSource(), def.getTarget());
}
try {
// fetch full work item information
List<IWorkItem> items = WorkItemHelpers.fetchWorkItems(clientWorkItemList, workItemServer, monitor);
// resolve project area
IProjectAreaHandle targetArea = ProjectAreaHelpers.getProjectArea(targetPA.getAsString(), parentService);
// prepare movement and track fields to be changed
MovePreparationResult preparationResult = mover.PrepareMove(items, targetArea, clientMappingDefinitions, typeMap);
// store attribute based ovservations to be able to return this information to the end user
moveResults = preparationResult.getAttributeDefinitions().getAttributeDefinitionCollection();
if(!previewOnly) {
// try to move the work items...
IStatus status = mover.MoveAll(preparationResult.getWorkItems());
isMoved = status.isOK();
}
} catch (Exception e) {
// Inform the user the the items could not be moved
responseJson.addProperty("error", e.getMessage());
}
// prepare data to be returend
responseJson.addProperty("successful", isMoved);
responseJson.add("mapping", gson.toJsonTree(moveResults, resultsType));
response.getWriter().write(gson.toJson(responseJson));
}