title: Using Java to decode JSON array of objects
body: <p>I have JSON as follows:</p>
  
  <pre><code>[{"0":"1","id":"1","1":"abc","name":"abc"},{"0":"2","id":"2","1":"xyz","name":"xyz"}]
  </code></pre>
  
  <p>It is an array of objects.</p>
  
  <p>I need to parse it using Java. I am using the library at :
  <a href="http://code.google.com/p/json-simple/downloads/list" rel="nofollow">http://code.google.com/p/json-simple/downloads/list</a></p>
  
  <p>Example 1 at this link approximates what I require:
  <a href="http://code.google.com/p/json-simple/wiki/DecodingExamples" rel="nofollow">http://code.google.com/p/json-simple/wiki/DecodingExamples</a></p>
  
  <p>I have the following code:</p>
  
  <pre><code>/** Decode JSON */
  // Assuming the JSON string is stored in jsonResult (String)
  
  Object obj = JSONValue.parse(jsonResult);
  JSONArray array = (JSONArray)obj;
  JSONObject jsonObj = null;
  for (int i=0;i&lt;array.length();i++){
      try {
          jsonObj = (JSONObject) array.get(i);
      } catch (JSONException e) {
          e.printStackTrace();
      } 
      try {
          Log.d(TAG,"Object no." + (i+1) + " field1: " + jsonObj.get("0") + " field2:       " + jsonObj.get("1"));
      } catch (JSONException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I am getting the following exception:</p>
  
  <pre><code>java.lang.ClassCastException: org.json.simple.JSONArray
  // at JSONArray array = (JSONArray)obj;
  </code></pre>
  
  <p>Can someone please help?</p>
  
  <p>Thanks.</p>