title: Why do I get the error "java.lang.String cannot be cast to java.util.List" with the following snippet?
body: <p>Here is a sample snippet where I am trying to get array list from a json object by I am getting class cast exception while doing this.</p>
  
  <pre><code>public static void main(String args[]) throws Exception {
      List&lt;String&gt; arrayList = new ArrayList&lt;String&gt;();
      arrayList.add("a");
      arrayList.add("b");
      arrayList.add("c");
      arrayList.add("d");
      arrayList.add("e");
      JSONObject json = new JSONObject();
      json.put("raja", "suba");
      json.put("arraylist", arrayList);
      System.out.println("Thus the value of array list is : "+json.get("arraylist"));
      List&lt;String&gt; oldlist = (List&lt;String&gt;) json.get("arraylist");
      System.out.println("Old list contains : "+oldlist);
      System.out.println("The old json contains : "+json.toString());
      String result = json.toString();
      JSONObject json1 = new JSONObject(result);
      System.out.println("The new json value is  : " +json1);
      System.out.println("The value in json for raja is  :" +json1.get("raja"));
      System.out.println("The vlaue of array list is  : "+json1.get("arraylist"));
      List&lt;String&gt; newlist = (List&lt;String&gt;) json1.get("arraylist");
      System.out.println("Thus the value of new list contains : "+newlist);
  }
  </code></pre>
  
  <p>I am not getting exception while obtaining <code>oldlist</code> but I'm getting class cast exception while obtaining <code>newlist</code>. </p>
  
  <ol>
  <li>Why the array list is treated as an object in the first case but as a string in the second case?</li>
  <li>What json.toString() really does? Does it converts the entire json object to string like appending double quotes "" to entire json object or does it converts each and every object within it into string?</li>
  </ol>