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


Java Gson.fromJson方法代码示例

本文整理汇总了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());
}
 
开发者ID:filestack,项目名称:filestack-java,代码行数:21,代码来源:TestStartResponse.java

示例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;
}
 
开发者ID:medvector,项目名称:educational-plugin,代码行数:27,代码来源:EduStepicAuthorizedClient.java

示例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;

    }
 
开发者ID:Cesarsk,项目名称:Say_it,代码行数:22,代码来源:FavoritesFragment.java

示例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;
    }
}
 
开发者ID:hsm59,项目名称:WatchIt,代码行数:23,代码来源:CustomSharedPreference.java

示例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);
}
 
开发者ID:ChresSSB,项目名称:Webcheckers,代码行数:28,代码来源:PostSubmitTurnRouteTest.java

示例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 ========================
}
 
开发者ID:LightSun,项目名称:data-mediator,代码行数:12,代码来源:GsonSinceUntilAnnotationsExample.java

示例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);
}
 
开发者ID:tututututututu,项目名称:BaseCore,代码行数:13,代码来源:GsonUtils.java

示例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();
    }
}
 
开发者ID:thane98,项目名称:3DSFE-Randomizer,代码行数:14,代码来源:AChapters.java

示例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));
  }
}
 
开发者ID:dehora,项目名称:outland,代码行数:10,代码来源:NamesapaceAuthServiceViaPlanBServer.java

示例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();
}
 
开发者ID:d2si-oss,项目名称:ooso,代码行数:10,代码来源:Launcher.java

示例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;
}
 
开发者ID:BITPlan,项目名称:can4eve,代码行数:17,代码来源:VehicleGroup.java

示例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;
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:31,代码来源:ForgeHooks.java

示例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];
}
 
开发者ID:capitalone,项目名称:Hydrograph,代码行数:39,代码来源:ViewExecutionHistoryUtility.java

示例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");
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:10,代码来源:MainActivity.java

示例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;
}
 
开发者ID:duanyaxin,项目名称:springboot-smart,代码行数:10,代码来源:RedisService.java


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