public ArrayList<JsonBean> parseData(String result) {//Gson 解析 ArrayList<JsonBean> detail = new ArrayList<>(); try { JSONArray data = new JSONArray(result); Gson gson = new Gson(); for (int i = 0; i < data.length(); i++) { JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class); detail.add(entity); } } catch (Exception e) { e.printStackTrace(); mHandler.sendEmptyMessage(MSG_LOAD_FAILED); } return detail; }
private String getJSONString(CruiseControlParameters parameters) { List<Map<String, Object>> jsonUserTaskList = new ArrayList<>(); Set<UUID> requestedUserTaskIds = ((UserTasksParameters) parameters).userTaskIds(); addFilteredJSONTasks(jsonUserTaskList, _activeUserTasks, ACTIVE_TASK_LABEL_VALUE, requestedUserTaskIds); addFilteredJSONTasks(jsonUserTaskList, _completedUserTasks, COMPLETED_TASK_LABEL_VALUE, requestedUserTaskIds); Map<String, Object> jsonResponse = new HashMap<>(); jsonResponse.put(USER_TASKS, jsonUserTaskList); jsonResponse.put(VERSION, JSON_VERSION); return new Gson().toJson(jsonResponse); }
List<DataObject> objList = new ArrayList<DataObject>(); objList.add(new DataObject(0, "zero")); objList.add(new DataObject(1, "one")); String json = new Gson().toJson(objList); System.out.println(json); List<DataObject> inpList = new Gson().fromJson(json, type); for (int i=0;i<inpList.size();i++) { DataObject x = inpList.get(i); System.out.println(x);
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<String> list = new ArrayList<>(); list.add("item1"); list.add("item2"); list.add("item3"); String json = new Gson().toJson(list); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(json); }
/** * Example to writeJson using TreeModel */ private static void writeJson() throws IOException { JsonObject rootObject = new JsonObject(); rootObject.addProperty("message", "Hi"); JsonObject childObject = new JsonObject(); childObject.addProperty("name", "World!"); rootObject.add("place", childObject); Gson gson = new Gson(); String json = gson.toJson(rootObject); System.out.println(json); // print "{"message":"Hi","place":{"name":"World!"}}" } }
@VisibleForTesting public static List<TestInformation> loadTestInformationFromFile() { try { List<TestInformation> testInformation = new ArrayList<>(); File testInformationFile = new File(getLocalVideosPath(), TEST_INFORMATION_FILE); if (testInformationFile.exists()) { List<String> lines = FileUtils.readLines(testInformationFile, UTF_8); Gson gson = new Gson(); for (String line : lines) { testInformation.add(gson.fromJson(line, TestInformation.class)); } } return testInformation; } catch (Exception e) { LOGGER.warn(e.toString(), e); } return new ArrayList<>(); }
@Override public synchronized String getClusterDef() { if (this.clusterDef.get(XLearningConstants.WORKER.toString()).size() == applicationContext.getWorkerNum() && this.clusterDef.get(XLearningConstants.PS.toString()).size() == applicationContext.getPsNum()) { if (this.clusterDefStr == null) { Collections.sort(this.clusterDef.get(XLearningConstants.PS.toString()), new compairIndex()); Collections.sort(this.clusterDef.get(XLearningConstants.WORKER.toString()), new compairIndex()); List workerList = new ArrayList<String>(); List psList = new ArrayList<String>(); for (int i = 0; i < this.clusterDef.get(XLearningConstants.WORKER.toString()).size(); i++) { workerList.add(this.clusterDef.get(XLearningConstants.WORKER.toString()).get(i).getHost()); } for (int i = 0; i < this.clusterDef.get(XLearningConstants.PS.toString()).size(); i++) { psList.add(this.clusterDef.get(XLearningConstants.PS.toString()).get(i).getHost()); } Map<String, List<String>> clusterMessage = new HashMap<>(); clusterMessage.put(XLearningConstants.WORKER, workerList); clusterMessage.put(XLearningConstants.PS, psList); LOG.info("Sending cluster def \"" + new Gson().toJson(clusterMessage) + "\"to container"); this.clusterDefStr = new Gson().toJson(clusterMessage); } } return this.clusterDefStr; }
List<Cars> cars= new ArrayList<Cars>(); cars.add(a); cars.add(b); cars.add(c); cars.add(d); gson = new Gson(); String jsonCars = gson.toJson(cars); Log.d("TAG","jsonCars = " + jsonCars);
static class PersonData { int age; String surname; public String toString() { return "[age = " + age + ", surname = " + surname + "]"; } } public static void main(String[] args) { String json = "{\"Thomas\": {\"age\": 32,\"surname\": \"Scott\"},\"Andy\": {\"age\": 25,\"surname\": \"Miller\"}}"; System.out.println(json); Gson gson = new Gson(); Map<String, PersonData> decoded = gson.fromJson(json, new TypeToken<Map<String, PersonData>>(){}.getType()); System.out.println(decoded); }
import java.io.FileReader; import java.util.ArrayList; import com.google.gson.Gson; public class Foo { public static void main(String[] args) throws Exception { Gson gson = new Gson(); TypeDTO[] myTypes = gson.fromJson(new FileReader("input.json"), TypeDTO[].class); System.out.println(gson.toJson(myTypes)); } } class TypeDTO { int id; String name; ArrayList<ItemDTO> items[]; } class ItemDTO { int id; String name; Boolean valid; }