Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are misunderstanding the way json serializes objects. Basically (very basically) <code>{...}</code> are objects, <code>[...,...]</code> are arrays and <code>key:value</code> are object properties, so something like:</p> <pre><code>{ "responseData": { "results": [], "cursor": { "moreResultsUrl": "http://www.url.com", "searchResultTime": "0,00" } }, "responseDetails": null, "responseStatus": 200 } </code></pre> <p>Means that you have a first object that has 3 properties: <code>responseData</code>(object), <code>responseDetails</code>(null object) and <code>responseStatus</code>(int). And <code>responseData</code> is an object with 2 properties: <code>results</code> (array) and <code>cursor</code>(object). And <code>cursor</code>'s properties are <code>moreResultsUrl</code>(string), and <code>searchResultTime</code>(string).</p> <p>and now, for parsing the json you can try something like this:</p> <ul> <li>Take in mind that your code provided was too cloned-from-tutorial. You will have to edit things a little bit more in order to maintain some grade of parameter naming (your variables talk about cars and your data about web searches...). And you may rename some data from <code>ListAdapter adapter = new Simple...</code> method in order to fit your needs.</li> </ul> <p>** In order to use these JSON methods, you need to include the simple json library.</p> <pre><code>import java.text.ParseException; import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.widget.ListAdapter; import android.widget.SimpleAdapter; public class MainActivity extends ListActivity { private Context context; private static String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&amp;q=pixelloid&amp;rsz=8"; private static final String TAG_content = "content"; private static final String TAG_GsearchResultClass = "GsearchResultClass"; private static final String TAG_titleNoFormatting = "titleNoFormatting"; private static final String TAG_title = "title"; private static final String TAG_cacheUrl = "cacheUrl"; private static final String TAG_unescapedUrl = "unescapedUrl"; private static final String TAG_url = "url"; private static final String TAG_visibleUrl = "visibleUrl"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new ProgressTask(MainActivity.this).execute(); } private class ProgressTask extends AsyncTask&lt;String, Void, Boolean&gt; { private ProgressDialog dialog; private ListActivity activity; // private List&lt;Message&gt; messages; public ProgressTask(ListActivity activity) { this.activity = activity; context = activity; dialog = new ProgressDialog(context); } /** progress dialog to show user that the backup is processing. */ /** application context. */ private Context context; protected void onPreExecute() { this.dialog.setMessage("Progress start"); this.dialog.show(); } @Override protected void onPostExecute(final Boolean success) { if (dialog.isShowing()) { dialog.dismiss(); } ListAdapter adapter = new SimpleAdapter( context, contents, R.layout.list_item, new String[] { TAG_title, TAG_content, TAG_url }, new int[] { R.id.vehicleType, R.id.vehicleColor, R.id.fuel, R.id.treadType }); setListAdapter(adapter); // selecting single ListView item //lv = getListView(); } protected Boolean doInBackground(final String... args) { loadJson(); return null; } } public static JSONObject parse() { JSONParser parser = new JSONParser(); Object obj; JSONObject json = new JSONObject(); try { obj = parser.getJSONFromUrl(url); json=(JSONObject)obj; } catch (ParseException e) { e.printStackTrace(); } return json; } private String moreResultsUrl = ""; private ArrayList&lt;HashMap&lt;String, String&gt;&gt; contents; public void loadJson() { try { JSONObject obj = parse(); if(obj!=null) { JSONObject responseData; responseData = (JSONObject ) obj.get("responseData"); if(responseData !=null) { JSONArray results = (JSONArray) obj.get("results"); contents = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); HashMap&lt;String, String&gt; map; JSONObject cro; for(int i=0;i&lt;results.length();i++) { map = new HashMap&lt;String, String&gt;(); cro = (JSONObject) results.get(i); map.put(TAG_content, cro.get(TAG_content).toString() ); map.put(TAG_GsearchResultClass, cro.get(TAG_GsearchResultClass).toString() ); map.put(TAG_titleNoFormatting, cro.get(TAG_titleNoFormatting).toString() ); map.put(TAG_titleNoFormatting, cro.get(TAG_titleNoFormatting).toString() ); map.put(TAG_title, cro.get(TAG_title).toString() ); map.put(TAG_cacheUrl, cro.get(TAG_cacheUrl).toString() ); map.put(TAG_unescapedUrl, cro.get(TAG_unescapedUrl).toString() ); map.put(TAG_url, cro.get(TAG_url).toString() ); map.put(TAG_visibleUrl, cro.get(TAG_visibleUrl).toString() ); contents.add(map); } JSONObject cursor = (JSONObject ) responseData.get("cursor"); if(cursor!=null) moreResultsUrl = cursor.get("moreResultsUrl").toString(); } } } catch (JSONException e) { e.printStackTrace(); } } </code></pre> <p>}</p>
 

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