本文整理汇总了Java中com.google.gson.JsonParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java JsonParser.parse方法的具体用法?Java JsonParser.parse怎么用?Java JsonParser.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.JsonParser
的用法示例。
在下文中一共展示了JsonParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import com.google.gson.JsonParser; //导入方法依赖的package包/类
/**
* Pings the server
*
* @source https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
* */
private void execute() throws Throwable {
//don't execute on debug-mode
if(this.debugging == true)
return;
String url = "https://redunda.sobotics.org/status.json";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", UserAgent.getUserAgent());
String parameters = "key="+this.apiKey;
//add version parameter if available
if (this.version != null)
parameters = parameters+"&version="+this.version;
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(parameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String responseString = response.toString();
//http://stackoverflow.com/a/15116323/4687348
JsonParser jsonParser = new JsonParser();
JsonObject object = (JsonObject)jsonParser.parse(responseString);
try {
boolean standbyResponse = object.get("should_standby").getAsBoolean();
boolean oldValue = PingService.standby.get();
PingService.standby.set(standbyResponse);
String newLocation = object.get("location").getAsString();
PingService.location = newLocation;
if (standbyResponse != oldValue) {
if (this.delegate != null)this.delegate.standbyStatusChanged(standbyResponse);
}
} catch (Throwable e) {
//no apikey or server might be offline; don't change status!
this.forwardError(e);
throw e;
}
}
示例2: LoadMovies
import com.google.gson.JsonParser; //导入方法依赖的package包/类
public void LoadMovies(String path) throws FileNotFoundException {
parser = new JsonParser();
Object obj = parser.parse(new FileReader(path));
JsonArray Filme = (JsonArray) obj;
for (JsonElement j : Filme) {
JsonObject jsonObject = j.getAsJsonObject();
FilmeSammlung.Filme.add(
new Film(
jsonObject.get("id").getAsString(),
jsonObject.get("name").getAsString(),
jsonObject.get("beschreibung").getAsString(),
jsonObject.get("dauer").getAsString(),
jsonObject.get("altersbegrenzung").getAsString(),
jsonObject.get("dreidfaehigkeit").getAsString(),
jsonObject.get("sprache").getAsString()
)
);
}
}
示例3: parseRandomArticle
import com.google.gson.JsonParser; //导入方法依赖的package包/类
/**
* Finds the random article in the specified JSON data.
* @return The title of a random Wikipedia article.
*/
public static String parseRandomArticle(String json) {
// Create a JSON Parser using GSON and parse the root of the JSON data
JsonParser jParser = new JsonParser();
JsonElement root = jParser.parse(json);
// Navigate Root -> Query -> Random
JsonElement jQuery = root.getAsJsonObject().get("query");
JsonElement jRandom = jQuery.getAsJsonObject().get("random");
// Fetch the random array in the JSON data and just get the first
// element.
JsonArray randomArray = jRandom.getAsJsonArray();
JsonObject first = randomArray.get(0).getAsJsonObject();
return first.get("title").getAsString();
}
示例4: match
import com.google.gson.JsonParser; //导入方法依赖的package包/类
@Override
public void match(MvcResult result) throws Exception {
String content = result.getResponse().getContentAsString();
final JsonParser parser = new JsonParser();
final JsonElement actual = parser.parse(content);
if (actual.isJsonPrimitive()) {
final JsonElement expected = parser.parse(expectedJsonResponse);
assertThat(actual, is(expected));
} else {
try {
JSONAssert.assertEquals(expectedJsonResponse, content, false);
} catch (AssertionError e) {
throw new ComparisonFailure(e.getMessage(), expectedJsonResponse, content);
}
}
}
示例5: downloadModel
import com.google.gson.JsonParser; //导入方法依赖的package包/类
private PlayerItemModel downloadModel(String p_downloadModel_1_)
{
String s = "http://s.optifine.net/" + p_downloadModel_1_;
try
{
byte[] abyte = HttpUtils.get(s);
String s1 = new String(abyte, "ASCII");
JsonParser jsonparser = new JsonParser();
JsonObject jsonobject = (JsonObject)jsonparser.parse(s1);
PlayerItemParser playeritemparser = new PlayerItemParser();
PlayerItemModel playeritemmodel = PlayerItemParser.parseItemModel(jsonobject);
return playeritemmodel;
}
catch (Exception exception)
{
Config.warn("Error loading item model " + p_downloadModel_1_ + ": " + exception.getClass().getName() + ": " + exception.getMessage());
return null;
}
}
示例6: parseLocation
import com.google.gson.JsonParser; //导入方法依赖的package包/类
public LocationDTO parseLocation(String result){
JsonParser parser = new JsonParser();
JsonObject json = (JsonObject) parser.parse(result);
JsonElement jsonCode = json.get("status");
String code = jsonCode.getAsString();
if(! (code.equals("OK"))){
return null;
}
JsonArray jsonArray = json.getAsJsonArray("results");
JsonElement elm = jsonArray.get(0);
JsonObject obj = elm.getAsJsonObject();
JsonObject ob2 = obj.getAsJsonObject("geometry");
JsonObject obj3 = ob2.getAsJsonObject("location");
String jsonString = obj3.toString();
Gson gson = new GsonBuilder().create();
LocationDTO loc = gson.fromJson(jsonString, LocationDTO.class);
return loc;
}
示例7: fileDownloadFinished
import com.google.gson.JsonParser; //导入方法依赖的package包/类
public void fileDownloadFinished(String p_fileDownloadFinished_1_, byte[] p_fileDownloadFinished_2_, Throwable p_fileDownloadFinished_3_)
{
if (p_fileDownloadFinished_2_ != null)
{
try
{
String s = new String(p_fileDownloadFinished_2_, "ASCII");
JsonParser jsonparser = new JsonParser();
JsonElement jsonelement = jsonparser.parse(s);
PlayerConfigurationParser playerconfigurationparser = new PlayerConfigurationParser(this.player);
PlayerConfiguration playerconfiguration = playerconfigurationparser.parsePlayerConfiguration(jsonelement);
if (playerconfiguration != null)
{
playerconfiguration.setInitialized(true);
PlayerConfigurations.setPlayerConfiguration(this.player, playerconfiguration);
}
}
catch (Exception exception)
{
Config.dbg("Error parsing configuration: " + p_fileDownloadFinished_1_ + ", " + exception.getClass().getName() + ": " + exception.getMessage());
}
}
}
示例8: parse
import com.google.gson.JsonParser; //导入方法依赖的package包/类
public GeoJSONObjectDto parse(String geoJson) {
if (geoJson == null) {
return null;
}
JsonParser parser = new JsonParser();
JsonElement parsed = parser.parse(geoJson);
JsonObject jsonObject = parsed.getAsJsonObject();
String typeString = jsonObject.get("type").getAsString();
GeoJSONObjectTypeEnum typeEnum = GeoJSONObjectTypeEnum.valueOf(typeString);
return (GeoJSONObjectDto) gson.fromJson(parsed, typeEnum.getDtoClass());
}
示例9: saveJson
import com.google.gson.JsonParser; //导入方法依赖的package包/类
public synchronized static void saveJson(String jsonContent, File to) {
try {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(jsonContent);
jsonContent = gson.toJson(je);
PrintWriter writer = new PrintWriter(to, "UTF-8");
writer.println(jsonContent);
writer.close();
} catch (Exception ex) {
error("Failed to save json");
}
}
示例10: MaxwellRecord
import com.google.gson.JsonParser; //导入方法依赖的package包/类
public MaxwellRecord(String changeValue) {
JsonParser jsonParser = new JsonParser();
JsonObject value = (JsonObject) jsonParser.parse(changeValue);
this.dataSource = getPipeLineName();
this.database = value.get("database").getAsString();
this.table = value.get("table").getAsString();
this.produceTime = value.get("ts").getAsLong();
this.data = value.get("data").getAsJsonObject();
if (value.has("old") && !value.get("old").isJsonNull()) {
this.old = value.get("old").getAsJsonObject();
}
switch (value.get("type").getAsString()) {
case "insert":
type = RowType.INSERT;
break;
case "update":
type = RowType.UPDATE;
break;
case "delete":
type = RowType.DELETE;
break;
}
}
示例11: getChacheFile
import com.google.gson.JsonParser; //导入方法依赖的package包/类
private JsonObject getChacheFile(Plugin plugin){
File file = new File(plugin.getDataFolder().getPath()+"/truenonpcdata.json");
if(file.exists()){
try{
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(new FileReader(file));
return jsonElement.getAsJsonObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}else return null;
}
示例12: readInjectedDependencies
import com.google.gson.JsonParser; //导入方法依赖的package包/类
private void readInjectedDependencies()
{
File injectedDepFile = new File(getConfigDir(),"injectedDependencies.json");
if (!injectedDepFile.exists())
{
FMLLog.getLogger().log(Level.DEBUG, "File {} not found. No dependencies injected", injectedDepFile.getAbsolutePath());
return;
}
JsonParser parser = new JsonParser();
JsonElement injectedDeps;
try
{
injectedDeps = parser.parse(new FileReader(injectedDepFile));
for (JsonElement el : injectedDeps.getAsJsonArray())
{
JsonObject jo = el.getAsJsonObject();
String modId = jo.get("modId").getAsString();
JsonArray deps = jo.get("deps").getAsJsonArray();
for (JsonElement dep : deps)
{
JsonObject depObj = dep.getAsJsonObject();
String type = depObj.get("type").getAsString();
if (type.equals("before")) {
injectedBefore.put(modId, VersionParser.parseVersionReference(depObj.get("target").getAsString()));
} else if (type.equals("after")) {
injectedAfter.put(modId, VersionParser.parseVersionReference(depObj.get("target").getAsString()));
} else {
FMLLog.getLogger().log(Level.ERROR, "Invalid dependency type {}", type);
throw new RuntimeException("Unable to parse type");
}
}
}
} catch (Exception e)
{
FMLLog.getLogger().log(Level.ERROR, "Unable to parse {} - skipping", injectedDepFile);
FMLLog.getLogger().throwing(Level.ERROR, e);
return;
}
FMLLog.getLogger().log(Level.DEBUG, "Loaded {} injected dependencies on modIds: {}", injectedBefore.size(), injectedBefore.keySet());
}
示例13: getPayload
import com.google.gson.JsonParser; //导入方法依赖的package包/类
@Override
public String [] getPayload (final PayloadType type, final String body) {
try {
final JsonParser parser = new JsonParser ();
final Gson gson = new GsonBuilder ().setPrettyPrinting ()
.create ();
final JsonElement element = parser.parse (body);
return gson.toJson (element)
.split ("\n");
}
catch (final Exception e) {
fail (JsonFormatTransformerError.class, "Error while JSON Transformation.", e);
}
return new String [] {};
}
示例14: GetTranslation
import com.google.gson.JsonParser; //导入方法依赖的package包/类
public static String GetTranslation(String s){
String res = "";
JsonParser parse = new JsonParser();
JsonObject json = (JsonObject) parse.parse(s);
res = json.get("translation").getAsString();
return res;
}
示例15: stringToJSONArray
import com.google.gson.JsonParser; //导入方法依赖的package包/类
public JsonArray stringToJSONArray(String response){
JsonParser parser = new JsonParser();
JsonElement tradeElement = parser.parse(response);
return tradeElement.getAsJsonArray();
}
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-revolverenguardia-1,代码行数:6,代码来源:testUtils.java