def toList(s, listid, dialname=None, listname=None): elem = JsonParser().parse(s) object = elem.getAsJsonObject() array = object.getAsJsonArray(listid) list = [] lform = None if dialname != None: lform = miscutil.toListMap(dialname, listname) for i in range(array.size()): ma = {} o = array.get(i) # print i,o s = o.entrySet() for e in s: # print e,e.getKey(),e.getValue() if e.getValue().isJsonNull(): val = None else: if lform != None: (ttype, after) = miscutil.getColumnDescr(lform, e.getKey()) p = o.getAsJsonPrimitive(e.getKey()) if p.isBoolean(): val = p.getAsBoolean() elif p.isNumber(): val = p.getAsDouble() if lform != None: if ttype == cutil.LONG: val = int(val) else: val = p.getAsString() if lform != None: if ttype == cutil.DATE: val = con.StoDate(val) if ttype == cutil.DATETIME: val = con.StoDate(val, True) ma[e.getKey()] = val list.append(ma) return list
*/ public void translateAString (String text) throws Exception { URL url = new URL (HOST + PATH + params); List<RequestBody> objList = new ArrayList<>(); objList.add(new RequestBody(text)); String content = new Gson().toJson(objList); translatedText = Post(url, content); } /* * This function was not written by the team. * Source: https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-java-translate */ public String prettify(String json_text) { JsonParser parser = new JsonParser(); JsonElement json = parser.parse(json_text); Gson gson = new GsonBuilder().setPrettyPrinting().create(); return gson.toJson(json); } /* * Returns this part of json_text: * "text":"[EVERYTHING IN HERE]","to": */ public String getTextFromJson (String json_text) { return json_text.substring(json_text.indexOf("\"text\":") + 8, json_text.indexOf("\",\"to\":")); } /* * Set the params global variable to be used in the next text translation.