List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
From source file:com.mot.ajaxwebapp.BaseActivity.java
License:Apache License
/** * convert an inputstream to JsonElement with JsonParser. * Throw exception if json convertion failed, caller will handled the exception and close the stream. */// ww w . ja va2 s .c om public static JsonElement unmarshall(InputStream jsonContent) throws Exception { JsonParser parser = new JsonParser(); JsonElement element = parser.parse(new InputStreamReader(jsonContent, "UTF-8")); if (element.isJsonObject()) { return element.getAsJsonObject(); } else if (element.isJsonArray()) { return element.getAsJsonArray(); } else { throw new Exception("Unknown content found in response." + element); } }
From source file:com.msdpe.authenticationdemo.LoggedInActivity.java
License:Apache License
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_logged_in); //get UI elements mLblUserIdValue = (TextView) findViewById(R.id.lblUserIdValue); mLblUsernameValue = (TextView) findViewById(R.id.lblUsernameValue); mBtnLogout = (Button) findViewById(R.id.btnLogout); mBtnTestNoRetry = (Button) findViewById(R.id.btnTestNoRetry); mBtnTestRetry = (Button) findViewById(R.id.btnTestRetry); mLblInfo = (TextView) findViewById(R.id.lblInfo); //Set click listeners mBtnLogout.setOnClickListener(logoutClickListener); mBtnTestNoRetry.setOnClickListener(testNoRetryClickListener); mBtnTestRetry.setOnClickListener(testRetryClickListener); AuthenticationApplication myApp = (AuthenticationApplication) getApplication(); AuthService authService = myApp.getAuthService(); mLblUserIdValue.setText(authService.getUserId()); //Fetch auth data (the username) on load authService.getAuthData(new TableJsonQueryCallback() { @Override/*w ww . jav a2 s . c o m*/ public void onCompleted(JsonElement result, int count, Exception exception, ServiceFilterResponse response) { if (exception == null) { JsonArray results = result.getAsJsonArray(); JsonElement item = results.get(0); mLblUsernameValue.setText(item.getAsJsonObject().getAsJsonPrimitive("UserName").getAsString()); } else { Log.e(TAG, "There was an exception getting auth data: " + exception.getMessage()); } } }); }
From source file:com.msdpe.storagedemo.StorageService.java
License:Apache License
/*** * Fetches all of the tables from storage */// ww w .j a v a 2 s. co m public void getTables() { mTableTables.where().execute(new TableJsonQueryCallback() { @Override public void onCompleted(JsonElement result, int count, Exception exception, ServiceFilterResponse response) { if (exception != null) { Log.e(TAG, exception.getCause().getMessage()); return; } JsonArray results = result.getAsJsonArray(); mTables = new ArrayList<Map<String, String>>(); //Loop through the results and get the name of each table for (int i = 0; i < results.size(); i++) { JsonElement item = results.get(i); Map<String, String> map = new HashMap<String, String>(); map.put("TableName", item.getAsJsonObject().getAsJsonPrimitive("TableName").getAsString()); mTables.add(map); } //Broadcast that tables have been loaded Intent broadcast = new Intent(); broadcast.setAction("tables.loaded"); mContext.sendBroadcast(broadcast); } }); }
From source file:com.msdpe.storagedemo.StorageService.java
License:Apache License
/*** * Gets all of the rows for a specific table * @param tableName/* w w w .jav a2 s . c om*/ */ public void getTableRows(String tableName) { //Executes a read request with parameters //We have to do it in this way to ensure it shows up correctly on the server mTableTableRows.execute(mTableTableRows.parameter("table", tableName), new TableJsonQueryCallback() { @Override public void onCompleted(JsonElement result, int count, Exception exception, ServiceFilterResponse response) { if (exception != null) { Log.e(TAG, exception.getCause().getMessage()); return; } //Loop through the results and add them to our local collection JsonArray results = result.getAsJsonArray(); mTableRows = new ArrayList<JsonElement>(); for (int i = 0; i < results.size(); i++) { JsonElement item = results.get(i); mTableRows.add(item); } //Broadcast that table rows have been loaded Intent broadcast = new Intent(); broadcast.setAction("tablerows.loaded"); mContext.sendBroadcast(broadcast); } }); }
From source file:com.mycompany.mavenfirst.webManager.java
public void gsontoJsonArray() { String str = "[{\"name\":\"Tom\",\"city\":\"Houston\",\"country\":\"USA\"},\n" + "{\"name\":\"Ady\",\"city\":\"Melbourne\",\"country\":\"Australia\"}]"; JsonElement json = new JsonParser().parse(str); JsonArray array = json.getAsJsonArray(); Iterator iterator = array.iterator(); List<Demographics> p = new ArrayList<>(); while (iterator.hasNext()) { JsonObject json2 = (JsonObject) iterator.next(); Gson gson = new Gson(); Demographics v = gson.fromJson(json2, Demographics.class); p.add(v);//from w ww . j av a 2 s . c o m } JOptionPane.showMessageDialog(null, p); System.out.println(p); }
From source file:com.networkmanagerapp.JSONParser.java
License:Apache License
/** * @exception FileNotFoundException, IOException, all handled internally * @param filename. The name of the JSON file to parse. * @return JSONParsingResults object containing an arraylist of JSONItems and a string array of item names *//*ww w .ja va 2 s .c o m*/ public JSONParsingResults returnParsedData(String filename) { List<JSONItem> items = new ArrayList<JSONItem>(); try { FileInputStream fin = NetworkManagerMainActivity.getInstance().openFileInput(filename.substring(1)); BufferedReader reader = new BufferedReader(new InputStreamReader(fin)); String line; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) != null) { builder.append(line); } JsonElement root = new JsonParser().parse(builder.toString()); JsonArray arr = root.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { JsonObject jo = arr.get(i).getAsJsonObject(); Set<Entry<String, JsonElement>> entry = jo.entrySet(); JSONItem item = new JSONItem(); Iterator<Entry<String, JsonElement>> iterator = entry.iterator(); while (iterator.hasNext()) { Entry<String, JsonElement> next = iterator.next(); item.getItemData().put(next.getKey(), next.getValue().toString()); } items.add(item); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } final ArrayList<String> itemArrayList = new ArrayList<String>(items.size()); for (int i = 0; i < items.size(); i++) { itemArrayList.add(items.get(i).getItemData().get("name")); } String[] names = new String[itemArrayList.size()]; itemArrayList.toArray(names); return new JSONParsingResults(names, items); }
From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java
License:Apache License
protected void perform(JsonObject obj) { Set<Entry<String, JsonElement>> entries = obj.entrySet(); Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries); for (Entry<String, JsonElement> entry : orgEntries) { JsonElement val = entry.getValue(); if (val.isJsonPrimitive() || val.isJsonNull()) { continue; }//from w w w .j a v a2s.c o m obj.remove(entry.getKey()); if (val.isJsonObject()) { perform(obj, val.getAsJsonObject(), entry.getKey()); } else if (val.isJsonArray()) { perform(obj, val.getAsJsonArray(), entry.getKey()); } } }
From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java
License:Apache License
protected void perform(JsonObject obj, JsonArray nested_arr, String parent) { int c = 0;//from ww w . j a v a 2s . c o m for (JsonElement val : nested_arr) { c += 1; String key = parent + separator + c; if (val.isJsonArray()) { perform(obj, val.getAsJsonArray(), key); } else if (val.isJsonObject()) { perform(obj, val.getAsJsonObject(), key); } else { obj.add(key, val); } } }
From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java
License:Apache License
protected void perform(JsonObject obj, JsonObject nested_obj, String parent) { Set<Entry<String, JsonElement>> entries = nested_obj.entrySet(); Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries); for (Entry<String, JsonElement> entry : orgEntries) { JsonElement val = entry.getValue(); String key = parent + separator + entry.getKey(); if (val.isJsonObject()) { perform(obj, val.getAsJsonObject(), key); } else if (val.isJsonArray()) { perform(obj, val.getAsJsonArray(), key); } else {/*w w w . j a v a2 s.c om*/ obj.add(key, val); } } }
From source file:com.nextdoor.bender.operation.json.key.KeyNameReplacementOperation.java
License:Apache License
protected void performOnArray(JsonElement elm) { if (elm.isJsonObject()) { perform(elm.getAsJsonObject());/*ww w.ja va 2 s. c o m*/ } else if (elm.isJsonArray()) { JsonArray arr = elm.getAsJsonArray(); arr.forEach(item -> { performOnArray(item); }); } }