Example usage for com.google.gson JsonElement getAsJsonObject

List of usage examples for com.google.gson JsonElement getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonObject.

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:com.dsh105.powermessage.core.PowerMessage.java

License:Open Source License

/**
 * Converts a raw JSON string to a PowerMessage object. This JSON string is in the format given by {@link #toJson()}
 * @param json the JSON string which represents a PowerMessage
 * @return A PowerMessage representing the given JSON string
 *//*  ww  w  . j  a  v a2  s  . c om*/
public static PowerMessage fromJson(String json) {
    JsonArray serialised = jsonParser.parse(json).getAsJsonObject().getAsJsonArray("extra");
    PowerMessage powerMessage = new PowerMessage();

    for (JsonElement serialisedElement : serialised) {
        PowerSnippet snippet = new PowerSnippet("");
        JsonObject message = serialisedElement.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : message.entrySet()) {
            String key = entry.getKey();
            JsonElement element = entry.getValue();

            if (key.equals("text")) {
                snippet.setText(element.getAsString());
            } else if (PowerSnippet.STYLE_TO_NAME_MAP.containsValue(key)) {
                if (element.getAsBoolean()) {
                    snippet.withColour(PowerSnippet.STYLE_TO_NAME_MAP.inverse().get(key));
                }
            } else if (key.equals("color")) {
                snippet.withColour(ChatColor.valueOf(element.getAsString().toUpperCase()));
            } else if (key.equals("clickEvent") || key.equals("hoverEvent")) {
                JsonObject event = element.getAsJsonObject();
                snippet.withEvent(key.substring(0, key.indexOf("Event")), event.get("action").getAsString(),
                        event.get("value").getAsString());
            }
        }
        powerMessage.then(snippet);
    }
    return powerMessage;
}

From source file:com.dspot.declex.localdb.User.java

License:Open Source License

    private java.util.Map<String, String> getAllFields(String fields) {      
     java.util.Map<String, String> allFields = new java.util.HashMap<>();
      /*from ww w.  ja v a2  s .  c  o  m*/
   try {
      JsonElement elem = getGson(this, fields).toJsonTree(this);
      for (Entry<String, JsonElement> entry : elem.getAsJsonObject().entrySet()) {
         String value = entry.getValue().toString();
         if (value.startsWith("\"")) value = value.substring(1, value.length()-1);
         allFields.put(entry.getKey(), value);
      }
   } catch(Exception e) {
      throw new RuntimeException(e);
   }
      
   return allFields;
}

From source file:com.easycode.visualisation_1.JSONParser.java

public JsonObject getJsonFromUrl(String sUrl) throws MalformedURLException, IOException {

    // Connect to the URL using java's native library
    URL url = new URL(sUrl);
    HttpURLConnection request = (HttpURLConnection) url.openConnection();
    request.connect();//from ww  w  . ja v a 2 s.  c o m

    // Convert to a JSON object to print data
    JsonParser jp = new JsonParser(); //from gson
    JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
    this.rootobj = root.getAsJsonObject(); //May be an array, may be an object. 

    return rootobj;
}

From source file:com.ecwid.mailchimp.MailChimpClient.java

License:Apache License

/**
 * Execute MailChimp API method.//from w w  w.j  a  va  2s  . com
 * 
 * @param method MailChimp API method to be executed
 * @return execution result
 */
public <R> R execute(MailChimpMethod<R> method) throws IOException, MailChimpException {
    final Gson gson = MailChimpGsonFactory.createGson();

    JsonElement result = execute(buildUrl(method), gson.toJsonTree(method));
    if (result.isJsonObject()) {
        JsonElement error = result.getAsJsonObject().get("error");
        if (error != null) {
            JsonElement code = result.getAsJsonObject().get("code");
            throw new MailChimpException(code.getAsInt(), error.getAsString());
        }
    }

    return gson.fromJson(result, method.getResultType());
}

From source file:com.eddy.malupdater.MalAPI.java

License:Open Source License

private static AnimeList jsonToAnimeList(JsonArray array) {
    AnimeList list = new AnimeList();
    for (JsonElement element : array) {
        Anime anime = jsonToAnime(element.getAsJsonObject());
        list.add(anime);//from   w w  w  .j  ava  2 s.com
    }
    return list;
}

From source file:com.edgebox.eacds.net.CDPostResponse.java

License:Open Source License

public static CDPostResponse build(String json) {
    CDPostResponse rt = new CDPostResponse();
    JsonElement e = new JsonParser().parse(json);
    rt.json = e.getAsJsonObject();
    rt.success = rt.json.get("success").getAsBoolean();
    rt.message = rt.json.get("message").getAsString();
    rt.errorTrace = rt.json.get("errorTrace").getAsString();

    try {/*from www.  j a  va  2  s. c  o  m*/
        rt.data = rt.json.getAsJsonObject("data").getAsJsonObject();
    } catch (java.lang.ClassCastException ex) {
        rt.data = rt.json.get("data").getAsString();
    }

    JsonElement dt = rt.json.get("data");
    if (dt.isJsonNull()) {
        rt.data = null;
    } else if (dt.isJsonObject()) {
        rt.data = dt.getAsJsonObject();
    } else {
        rt.data = dt.getAsJsonPrimitive();
    }
    return rt;
}

From source file:com.edu.ufps.maregroups.controller.negocio.Negocio.java

public boolean registrarProyecto(ProyectoDTO dto, String autores) throws Exception {

    boolean pro = false;
    IProyectoInvestigadorDAO proyec;/*  ww  w.  ja v a 2  s. c om*/
    ArrayList<InvestigadorDTO> investigadores;
    InvestigadorDTO investigador;
    JsonArray jsonArray;
    Gson gson = new Gson();
    String tipoDoc, numDoc;
    ProyectoInvestigadorDTO proyecto;

    try {
        jsonArray = gson.fromJson(autores, JsonArray.class);
        investigadores = new ArrayList<>(0);

        for (JsonElement jsonElement : jsonArray) {
            tipoDoc = jsonElement.getAsJsonObject().get("tipoDoc").getAsString();
            numDoc = jsonElement.getAsJsonObject().get("numDoc").getAsString();
            investigador = new InvestigadorDTO();
            investigador.setTipoDoc(tipoDoc);
            investigador.setNumDoc(numDoc);
            investigadores.add(investigador);
        }

        proyec = Negocio.factory.obtenerProyectoInvestigador();
        proyecto = new ProyectoInvestigadorDTO(dto);
        proyecto.setInvestigadores(investigadores);
        pro = proyec.registrarProyecto(proyecto);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return pro;

}

From source file:com.edu.ufps.maregroups.controller.negocio.Negocio.java

public boolean registrarArticulo(ArticuloDTO art, String investigadoresString) throws Exception {

    IArticuloDAO artDAO = Negocio.factory.obtenerArticulo();
    ArrayList<InvestigadorDTO> investigadores;
    InvestigadorDTO investigador;//from  w  w w .  j a v  a2s  .  co  m
    JsonArray jsonArray;
    Gson gson = new Gson();
    String tipoDoc, numDoc;

    jsonArray = gson.fromJson(investigadoresString, JsonArray.class);
    investigadores = new ArrayList<>(0);

    for (JsonElement jsonElement : jsonArray) {
        tipoDoc = jsonElement.getAsJsonObject().get("tipoDoc").getAsString();
        numDoc = jsonElement.getAsJsonObject().get("numDoc").getAsString();
        investigador = new InvestigadorDTO();
        investigador.setTipoDoc(tipoDoc);
        investigador.setNumDoc(numDoc);
        investigadores.add(investigador);
    }

    return artDAO.insertarArticulo(art, investigadores);
}