Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse Sub JSONArray and display image?
    primarykey
    data
    text
    <p>I am a php programmer and I am trying to learn JAVA. I have problem trying to parse a json file. It's been for 3 days..having a headache at this. I am create an android application (using JAVA) to parse json file. This is the sample json data (<a href="http://metalgearrisingguide.com/samplejson.json" rel="nofollow">http://metalgearrisingguide.com/samplejson.json</a>) And how to display the image?</p> <p>When i tried, I have the following error at my logcat and nothing is displayed on the emulator</p> <pre><code>Error parsing data org.json.JSONException: Value {"product":[{"summary":"Speak a word and the knob will turn by itself","state":"good","image":"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/e\/e9\/Door_Knob_with_Lock_USA.jpg","title":"Special Door knob requires no holding just music","address":[{"address":"Merchant address 1"},{"address":"merchant address 2"}],"url":"http:\/\/merchantwebsite.com"},{"summary":"This door bell will detect faces of known people and change music if unknown","state":"good","image":"http:\/\/www.thedoorbell.net\/images\/DoorbellHeader.jpg","title":"Special Door Bell that uses face recognition technology","address":[{"address":"Merchant address 1"},{"address":"merchant address 2"}],"url":"http:\/\/merchantwebsite2.com"},{"summary":"Can't wake up in the morning?\r\nWill you be awake for party? Listen to your favourite song that will awake you!","state":"refurnished","image":"http:\/\/jgrundig.wpengine.netdna-cdn.com\/wp-content\/uploads\/2011\/08\/RCA-Alarm-Clock-with-Charging-Cord-RC107.jpg","title":"The only alarm clock that plays your favourite song from your smartphone","address":[{"address":"Merchant address 1"},{"address":"merchant address 2"}],"url":"http:\/\/merchantwebsite3.com"}],"site":{"sitename":"Simple Product Find","imgprefix":"http:\/\/mydomain.com\/","urlprefix":"http:\/\/mydomain.com\/"}} at data of type org.json.JSONObject cannot be converted to JSONArray </code></pre> <p>Here is my JSONfunctions class:</p> <pre><code>public class JSONfunctions { public static JSONObject getJSONfromURL(String url){ InputStream is = null; String result = ""; JSONObject jArray = null; //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } try{ jArray = new JSONObject(result); }catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } return jArray; //return result; } } </code></pre> <p>And here is my code:</p> <pre><code>public class Main extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listplaceholder); ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); JSONObject json = JSONfunctions.getJSONfromURL("http://metalgearrisingguide.com/samplejson.json"); try{ JSONArray data = json.getJSONArray("data"); JSONArray products = data.getJSONArray(1); for(int i=0;i&lt;products.length();i++){ HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); JSONObject e = products.getJSONObject(i); map.put("id", String.valueOf(i)); map.put("title", "Product name:" + e.getString("title")); map.put("state", "Condition:" + e.getString("state")); map.put("summary", "Description: " + e.getString("summary")); mylist.add(map); Set set = map.entrySet(); // Get an iterator Iterator o = set.iterator(); // Display elements while(o.hasNext()) { Map.Entry me = (Map.Entry)o.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); } }catch(JSONException e) { Log.e("log_tag", "Error parsing data "+e.toString()); } ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "state", "summary" }, new int[] { R.id.item_title, R.id.item_subtitle }); setListAdapter(adapter); final ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { @SuppressWarnings("unchecked") HashMap&lt;String, String&gt; o = (HashMap&lt;String, String&gt;) lv.getItemAtPosition(position); Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); } }); } } </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.
 

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