1

Is it possible to Create JSON Parser more generic ?

Without mentioning any parent or child node tag in the code, I need to parse the JSON file and display it in a Android Activity.

Regards, Deepak Krishnan

user1273438
  • 11
  • 1
  • 2

1 Answers1

1

You can define your own json parser class-

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static JSONArray jsonArray = null;
    static String json = "";
    // constructor
    public JSONParser() {

    }    
        public JSONArray getJSONFromUrl(String url, List<NameValuePair> params) {    
            System.out.println("url:: "+url );
            System.out.println("params:: "+ params +" " +params.get(0) );
            // Making HTTP request
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                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);
                }
                is.close();
                json = sb.toString();
                //Log.e("JSON::: ", json);
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }      

            // try parse the string to a JSON object
            try {
                if(!json.equals("null")){
                    jsonArray = new JSONArray(json);
                     Log.d("jsonArray:: ",  jsonArray+"");
                }else{
                    jsonArray = null;
                }

            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }


            // return JSON String
            return jsonArray;

        }

      }

And your calling class like-

public class UserFuctions {
    private JSONParser jsonParser;

     // constructor
    public UserFuctions(){
        jsonParser = new JSONParser();
    }

    private static String HOST_URL = "http://100.43.0.21/pharmacy";
    public static final String FS  = File.separator;
    private static String genericListByNameSearchURL    = HOST_URL+FS +"getGenericByName.php";

    /**
     * 
     * function make Login Request
     * @param email
     * @param password
     * */

    public JSONArray getGenericByName(String genName){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();        
        params.add(new BasicNameValuePair("genName", genName));       
        JSONArray json = jsonParser.getJSONFromUrl(getGenericByName, params);

        return json;
    }


}
Priyanka
  • 677
  • 5
  • 20
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
  • HI @Mohammod Hossain , Here in my scenario I need to parse the .json file which is present in the Asset folder of Android. Am very new to this parsing and android. It will very helpful if you provide any inputs for this scenario. Thanks in advance.\ – user1273438 Jul 23 '13 at 10:04
  • @Mohammod Hossain I think you have to pass **genericListByNameSearchURL** instead of **getGenericByName** in `jsonParser.getJSONFromUrl(getGenericByName, params);` – Nitish Aug 12 '14 at 07:14