Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As far as i know there is no specific way to tell whether you are getting json array or json object after you parse it!!!.</p> <p>The way i would solve this,is to look for starting character of the string data from response.</p> <p>In your getJSONFromUrl method you get the string from response and then you start parse it into an JsonObject. Before you parse look for starting character based on starting character decide whether you are going for JsonObject or JsonArray.</p> <p>Am not sure if their is any shorter way to solve this but your problem is a tricky one :) .A lot of other things have to be dealt with since you don't actually know beforehand whether you are going to get jsonobject or jsonarray.</p> <p>Here is what i have put together:</p> <pre><code>public interface JsonListener { public boolean isJsonObejct(); } </code></pre> <p>Create a class by extending JSONObject and implementing the above interface.</p> <pre><code>import org.json.JSONException; import org.json.JSONObject; public class CustomJSONObject extends JSONObject implements JsonListener { public CustomJSONObject(String json) throws JSONException { // TODO Auto-generated constructor stub super(json); } @Override public boolean isJsonObejct() { // TODO Auto-generated method stub return true; } } </code></pre> <p>Create another class by extending JSONArray and implementing the above interface.</p> <pre><code>import org.json.JSONArray; import org.json.JSONException; public class CustomJSONArray extends JSONArray implements JsonListener{ public CustomJSONArray(String json) throws JSONException { // TODO Auto-generated constructor stub super(json); } @Override public boolean isJsonObejct() { // TODO Auto-generated method stub return false; } } </code></pre> <p>Modify you jsonparseclass so it does not return jsonobject or jsonarray instead it will return Object of type JsonListener.</p> <pre><code>public class JSONParser { static InputStream is = null; static JsonListener jObj = null; static String json = ""; // constructor public JSONParser() { } public JsonListener getJSONFromUrl(String url) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 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(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { if(json.startsWith("{")){ jObj = new CustomJSONObject(json); }else{ jObj = new CustomJSONArray(json); } } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } } </code></pre> <p>Now adjust your code to use those:</p> <pre><code>// Creating new JSON Parser JSONParser jParser = new JSONParser(); // Getting JSON from URL JsonListener json = jParser.getJSONFromUrl(url); if(json.isJsonObejct()){ Log.i("Got","JsonObject "+((CustomJSONObject)json).toString()); }else{ Log.i("Got","JsonArray "+((CustomJSONArray)json).toString()); } </code></pre> <p>Hopefully you will get some insight from this.</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