use of com.google.gson.JsonParser in project iosched by google.
the class DataExtractorTest method setUp.
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
fakeFetcher = new EntityFetcher() {
@Override
public JsonElement fetch(Enum<?> entityType, Map<String, String> params) throws IOException {
String filename = "sample_" + entityType.name();
if (params != null && params.get("page") != null && Integer.parseInt(params.get("page")) > 1) {
filename += "_page" + params.get("page");
}
filename += ".json";
InputStream stream = TestHelper.openTestDataFileStream(filename);
JsonReader reader = new JsonReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
return new JsonParser().parse(reader);
}
};
RemoteFilesEntityFetcherFactory.setBuilder(new FetcherBuilder() {
@Override
public FetcherBuilder setSourceFiles(String... filenames) {
return this;
}
@Override
public EntityFetcher build() {
return fakeFetcher;
}
});
sources = new ExtraInput().fetchAllDataSources();
sources.putAll(new VendorDynamicInput(fakeFetcher).fetchAllDataSources());
}
use of com.google.gson.JsonParser in project iosched by google.
the class CloudFileManager method readFileAsJsonObject.
public JsonObject readFileAsJsonObject(GcsFilename file) throws IOException {
GcsFileMetadata metadata = gcsService.getMetadata(file);
if (metadata == null) {
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Development) {
// In the development server, try to fetch files on cloud storage via HTTP
Logger.getAnonymousLogger().info("fetching " + file.getObjectName() + " at " + Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
return RemoteJsonHelper.fetchJsonFromPublicURL(Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
}
return null;
}
GcsInputChannel readChannel = null;
try {
readChannel = gcsService.openReadChannel(file, 0);
JsonElement element = new JsonParser().parse(Channels.newReader(readChannel, DEFAULT_CHARSET_NAME));
return element.getAsJsonObject();
} finally {
if (readChannel != null) {
readChannel.close();
}
}
}
use of com.google.gson.JsonParser in project SimpleNews by liuling07.
the class NewsJsonUtils method readJsonNewsDetailBeans.
public static NewsDetailBean readJsonNewsDetailBeans(String res, String docId) {
NewsDetailBean newsDetailBean = null;
try {
JsonParser parser = new JsonParser();
JsonObject jsonObj = parser.parse(res).getAsJsonObject();
JsonElement jsonElement = jsonObj.get(docId);
if (jsonElement == null) {
return null;
}
newsDetailBean = JsonUtils.deserialize(jsonElement.getAsJsonObject(), NewsDetailBean.class);
} catch (Exception e) {
LogUtils.e(TAG, "readJsonNewsBeans error", e);
}
return newsDetailBean;
}
use of com.google.gson.JsonParser in project SimpleNews by liuling07.
the class WeatherJsonUtils method getCity.
/**
* 从定位的json字串中获取城市
* @param json
* @return
*/
public static String getCity(String json) {
JsonParser parser = new JsonParser();
JsonObject jsonObj = parser.parse(json).getAsJsonObject();
JsonElement status = jsonObj.get("status");
if (status != null && "OK".equals(status.getAsString())) {
JsonObject result = jsonObj.getAsJsonObject("result");
if (result != null) {
JsonObject addressComponent = result.getAsJsonObject("addressComponent");
if (addressComponent != null) {
JsonElement cityElement = addressComponent.get("city");
if (cityElement != null) {
return cityElement.getAsString().replace("市", "");
}
}
}
}
return null;
}
use of com.google.gson.JsonParser in project SimpleNews by liuling07.
the class ImageJsonUtils method readJsonImageBeans.
/**
* 将获取到的json转换为图片列表对象
* @param res
* @return
*/
public static List<ImageBean> readJsonImageBeans(String res) {
List<ImageBean> beans = new ArrayList<ImageBean>();
try {
JsonParser parser = new JsonParser();
JsonArray jsonArray = parser.parse(res).getAsJsonArray();
for (int i = 1; i < jsonArray.size(); i++) {
JsonObject jo = jsonArray.get(i).getAsJsonObject();
ImageBean news = JsonUtils.deserialize(jo, ImageBean.class);
beans.add(news);
}
} catch (Exception e) {
LogUtils.e(TAG, "readJsonImageBeans error", e);
}
return beans;
}
Aggregations