本文整理汇总了Java中com.google.gson.Gson.fromJson方法的典型用法代码示例。如果您正苦于以下问题:Java Gson.fromJson方法的具体用法?Java Gson.fromJson怎么用?Java Gson.fromJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.Gson
的用法示例。
在下文中一共展示了Gson.fromJson方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import com.google.gson.Gson; //导入方法依赖的package包/类
@Test
public void test() {
Gson gson = new Gson();
String jsonString = "{"
+ "'uri' : '<uri>',"
+ "'region' : '<region>',"
+ "'location_url' : '<location_url>',"
+ "'upload_id' : '<upload_id>',"
+ "'upload_type' : 'intelligent_ingestion'"
+ "}";
StartResponse response = gson.fromJson(jsonString, StartResponse.class);
Map<String, RequestBody> params = response.getUploadParams();
Assert.assertTrue(params.containsKey("uri"));
Assert.assertTrue(params.containsKey("region"));
Assert.assertTrue(params.containsKey("upload_id"));
Assert.assertTrue(response.isIntelligent());
}
示例2: getTokens
import com.google.gson.Gson; //导入方法依赖的package包/类
@Nullable
private static StepicWrappers.TokenInfo getTokens(@NotNull final List<NameValuePair> parameters) {
final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
final HttpPost request = new HttpPost(EduStepicNames.TOKEN_URL);
request.setEntity(new UrlEncodedFormEntity(parameters, Consts.UTF_8));
try {
final CloseableHttpClient client = EduStepicClient.getHttpClient();
final CloseableHttpResponse response = client.execute(request);
final StatusLine statusLine = response.getStatusLine();
final HttpEntity responseEntity = response.getEntity();
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
EntityUtils.consume(responseEntity);
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
return gson.fromJson(responseString, StepicWrappers.TokenInfo.class);
}
else {
LOG.warn("Failed to get tokens: " + statusLine.getStatusCode() + statusLine.getReasonPhrase());
}
}
catch (IOException e) {
LOG.warn(e.getMessage());
}
return null;
}
示例3: loadDeserializedFavs
import com.google.gson.Gson; //导入方法依赖的package包/类
public static ArrayList<Pair<String, String>> loadDeserializedFavs(Context context) {
UtilitySharedPrefs.loadFavs(context);
ArrayList<String> SerializedFavs = new ArrayList<>(MainActivity.FAVORITES);
ArrayList<Pair<String, String>> DeserializedFavs = new ArrayList<>();
Gson gson = new Gson();
for (String element : SerializedFavs) {
SayItPair pair = gson.fromJson(element, SayItPair.class);
DeserializedFavs.add(pair);
}
Collections.sort(DeserializedFavs, new Comparator<Pair<String, String>>() {
@Override
public int compare(Pair<String, String> pair1, Pair<String, String> pair2) {
return pair1.first.compareTo(pair2.first);
}
});
return DeserializedFavs;
}
示例4: getTvGenrePreference
import com.google.gson.Gson; //导入方法依赖的package包/类
public List<Genre> getTvGenrePreference() {
Gson gson = new Gson();
String json = preference.getString("TV_GENRE_DATA", "");
Type type = new TypeToken<List<Genre>>() {
}.getType();
List<Genre> genreList = gson.fromJson(json, type);
if (BuildConfig.DEBUG) {
Log.d(TAG, "getGenrePreference: Genre List " + genreList);
}
if(genreList!=null) {
if (genreList.size() != 0) {
return genreList;
} else {
return null;
}
} else {
return null;
}
}
示例5: no_submit_turn_test
import com.google.gson.Gson; //导入方法依赖的package包/类
/**
* Test that PostSubmitTurn will return Message object with proper information when there are no pending moves
*/
@Test
public void no_submit_turn_test() {
// Arrange the test scenario: The session holds player
Player player = PlayerLobby.getPlayer("player");
when(session.attribute(PostSigninRoute.PLAYER_KEY)).thenReturn(player);
final Response response = mock(Response.class);
gson = new Gson();
// Invoke the test
Message message = gson.fromJson((String) CuT.handle(request, response), Message.class);
// Analyze the results:
// type is a non-null Type
final Message.Type type = message.getType();
assertNotNull(type);
assertTrue(type instanceof Message.Type);
// text is a non-null String
final String text = message.getText();
assertNotNull(text);
assertTrue(text instanceof String);
// model contains all necessary View-Model data
assertEquals(Message.Type.error, type);
assertEquals("No moves to submit", text);
}
示例6: testList
import com.google.gson.Gson; //导入方法依赖的package包/类
private static void testList() {
TestGsonBean tl = newTestGsonBean();
Gson gson = new Gson();
String json = gson.toJson(tl);
GsonTest.log(json);
TestGsonBean bean = gson.fromJson(json, TestGsonBean.class);
GsonTest.log(bean);
//========================= above ok ========================
}
示例7: jsonStrToMap
import com.google.gson.Gson; //导入方法依赖的package包/类
/**
* 将Json字符串转化为Map
* jsonStr Json字符串
*
* @return String
*/
public static <T> Map<String, T> jsonStrToMap(String jsonStr) {
Gson gson = new Gson();
Type type = new TypeToken<Map<String, T>>() {
}.getType();
return gson.fromJson(jsonStr, type);
}
示例8: AChapters
import com.google.gson.Gson; //导入方法依赖的package包/类
private AChapters() {
Type chapterType = new TypeToken<List<Chapter>>() {}.getType();
try {
Gson gson = new Gson();
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(Randomizer.class
.getResourceAsStream("data/json/AwakeningChapters.json"), "UTF-8")));
chapters = gson.fromJson(reader, chapterType);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例9: toTokenInfo
import com.google.gson.Gson; //导入方法依赖的package包/类
private Map<String, Object> toTokenInfo(Response response, Gson gson) {
try {
return gson.fromJson(response.body().string(), new TypeToken<Map<String, Object>>() {
}.getType());
} catch (IOException e) {
throw new ServiceException(
Problem.clientProblem("upstream_auth_parse_failed", e.getMessage(), 400));
}
}
示例10: loadJobInfo
import com.google.gson.Gson; //导入方法依赖的package包/类
private void loadJobInfo() throws IOException {
this.logger.info("Loading Job Configuration...");
Gson gson = new Gson();
InputStream driverInfoStream = getClass().getClassLoader().getResourceAsStream("jobInfo.json");
BufferedReader driverInfoReader = new BufferedReader(new InputStreamReader(driverInfoStream));
this.jobInfo = gson.fromJson(driverInfoReader, JobInfo.class);
driverInfoReader.close();
}
示例11: fromJsonStream
import com.google.gson.Gson; //导入方法依赖的package包/类
/**
* get the VehicleGroup from the given Json Stream
*
* @param jsonStream
* @param asTree
* @return
* @throws Exception
*/
public static VehicleGroup fromJsonStream(InputStream jsonStream)
throws Exception {
Gson gson = getGsonStatic();
VehicleGroup vehicleGroup = gson.fromJson(new InputStreamReader(jsonStream),
VehicleGroup.class);
vehicleGroup.reinit();
return vehicleGroup;
}
示例12: loadLootTable
import com.google.gson.Gson; //导入方法依赖的package包/类
public static LootTable loadLootTable(Gson gson, ResourceLocation name, String data, boolean custom)
{
Deque<LootTableContext> que = lootContext.get();
if (que == null)
{
que = Queues.newArrayDeque();
lootContext.set(que);
}
LootTable ret = null;
try
{
que.push(new LootTableContext(name, custom));
ret = gson.fromJson(data, LootTable.class);
que.pop();
}
catch (JsonParseException e)
{
que.pop();
throw e;
}
if (!custom)
ret = ForgeEventFactory.loadLootTable(name, ret);
if (ret != null)
ret.freeze();
return ret;
}
示例13: readJsonLogFile
import com.google.gson.Gson; //导入方法依赖的package包/类
/**
* Return last execution tracking status from tracking log file.
* @param uniqueJobId
* @param isLocalMode
* @param filePath
* @return
* @throws FileNotFoundException
*/
public ExecutionStatus readJsonLogFile(String uniqueJobId, boolean isLocalMode, String filePath, boolean isReplay) throws IOException{
ExecutionStatus[] executionStatus;
String jobId = "";
String path = null;
String jsonArray = "";
if(isLocalMode){
jobId = EXECUTION_TRACKING_LOCAL_MODE + uniqueJobId;
}else{
jobId = EXECUTION_TRACKING_REMOTE_MODE + uniqueJobId;
}
if(isReplay){
path = filePath + jobId + EXECUTION_TRACKING_LOG_FILE_EXTENTION;
}else{
path = getLogPath() + jobId + EXECUTION_TRACKING_LOG_FILE_EXTENTION;
}
JsonParser jsonParser = new JsonParser();
Gson gson = new Gson();
try(Reader fileReader = new FileReader(new File(path));){
jsonArray = jsonParser.parse(fileReader).toString();
}catch (Exception exception) {
logger.error("Failed to read file: ", exception);
}
executionStatus = gson.fromJson(jsonArray, ExecutionStatus[].class);
return executionStatus[executionStatus.length-1];
}
示例14: onComplete
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public void onComplete(Object o) {
JSONObject jsonObject = (JSONObject) o;
String json = jsonObject.toString();
Gson gson = new Gson();
TencentLoginResult result = gson.fromJson(json, TencentLoginResult.class);
LogUtils.e(result.toString());
mPresenter.login(result.openid, result.access_token, "QQ");
}
示例15: getUserListByKey
import com.google.gson.Gson; //导入方法依赖的package包/类
public List<User> getUserListByKey(String key){
Gson gson = new Gson();
List<User> userList = null;
String userJson = redisTemplate.opsForValue().get(key);
if(StringUtils.isNotEmpty(userJson)){
userList = gson.fromJson(userJson, new TypeToken<List<User>>(){}.getType() );
}
return userList;
}