Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting String of Json to JsonObject
    primarykey
    data
    text
    <p>I have a String of Json received from a web service http get request. I would like to turn the string that I have formed into a JsonObject.</p> <p>The question has been answered perfectly. Please see answer below.</p> <pre class="lang-java prettyprint-override"><code>import java.lang.reflect.Method; import java.lang.reflect.Type; import java.net.URL; import java.net.URLConnection; import java.net.HttpURLConnection; import java.util.List; import java.io.*; import java.util.*; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class RetrieveData { public static String httpGet(String urlStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if (conn.getResponseCode() != 200) { throw new IOException(conn.getResponseMessage()); } // Buffer the result into a string BufferedReader rd = new BufferedReader(new InputStreamReader( conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); return sb.toString(); } public static void main(String[] args) { try { String jsonString = (httpGet("http://kinseyreporter.org/API/list/reports/?tag=male%20victim&amp;from=2012-06-02")); JsonObject obj = new JsonObject(); Gson gson = new Gson(); System.out.println(jsonString); List&lt;String&gt; list = new ArrayList&lt;String&gt;(); gson.fromJson(jsonString, (Type) list); String json = gson.toJson(jsonString); System.out.println(json); } catch (Exception E) { } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload