title: parse JSON format using GSON?
body: <p>With HTTPURLCONNECTION I am able to get the JSON response and using Writer I am able to save it to output.json file also.  But I am not able to read the content of output.json or directly from the url "<a href="http://somesite.com/json/server.json" rel="nofollow">http://somesite.com/json/server.json</a>" using GSON. I am facing few issues when using gson. </p>
  
  <pre><code>public class ConnectToUrlUsingBasicAuthentication {
  
      public static void main(String[] args) {
  
          try {
              String webPage = "http://somesite.com/json/server.json";
              //HTTPURLCONNECTION
              URL url = new URL(webPage);
              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
              conn.setRequestMethod("GET");
              conn.setRequestProperty("Accept", "application/json");
              StringBuilder sb = new StringBuilder();
              InputStream is = conn.getInputStream();
              InputStreamReader isr = new InputStreamReader(is,Charset.defaultCharset());
              BufferedReader bufferedReader = new BufferedReader(isr);
              String line;
              while((line = bufferedReader.readLine()) != null) {
                  System.out.println("*** BEGIN ***");
                  try(Writer writer = new OutputStreamWriter(new FileOutputStream("Output.json") , "UTF-8")){
                      Gson gson = new GsonBuilder().create();
                      gson.toJson(line, writer);
                      System.out.println("Written successfully");
                  }
                  System.out.println(line);
                  System.out.println("*** END ***");
                  try(Reader reader = new InputStreamReader(is, "UTF-8")){
                      Gson gson = new GsonBuilder().create();
                      JsonData p = gson.fromJson(reader, JsonData.class);
                      System.out.println(p);
                  }
              }
  
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p><strong>The other class I am passing during gson.fromjson call is Jsondata.</strong> </p>
  
  <pre><code>public class JsonData {
      private String body;
      private List&lt;String&gt; items = new ArrayList&lt;String&gt;();
      private List&lt;String&gt; messages = new ArrayList&lt;String&gt;();
  
      // Getters and setters are not required for this example.
      // GSON sets the fields directly using reflection.
  
      @Override
      public String toString() {
          return messages + " - " + items + " - " + messages ;
      }
  }
  </code></pre>
  
  <p>Outputs:</p>
  
  <p>Json format (Is the JSON format looks fine or any syntax error is there in it)</p>
  
  <pre><code>line = {
      "body":
          {"items":[
              {"name":"server","state":"RUNNING","health":"HEALTH_OK"},
              {"name":"server1","state":"RUNNING","health":"HEALTH_OK"},
              {"name":"server2","state":"RUNNING","health":"HEALTH_OK"}
          ]},
  
      "messages":[]}
  </code></pre>
  
  <p>Value printed for variable p is null.</p>
  
  <p>Could some one please help me in printing the Json response in variable p using Gson.</p>