Sunday, March 6, 2016

CREATING AND READING JSON FILE

CREATING JSON FILE


        public static void StrTOJson(String imagetxt) throws JSONException
        {
           
            JSONObject obj = new JSONObject();
           
            obj.put("imagename",imgname);
            obj.put("image-bin", imagetxt);   
            obj.put("Result","success");
            try {
               
                FileWriter file = new FileWriter("D://imgeparser.json");
                file.write(obj.toString());
                System.out.println("Successfully Copied JSON Object to File...");
                System.out.println("\nJSON Object: " + obj);
                file.close();
            }
            catch(Exception ex)
            {
                System.out.println("ERROR.."+ex.getMessage());
            }
           
        }



READING JSON FILE:

 

        public static String ReadJson()
        {
          
             String imagbin="";
                String srcUrl = "D://imgeparser.json";
                String line;
                String jsondata="";
                StringBuffer stringBuffer = new StringBuffer();
                JSONObject jsonObj=null,jsonOut=null;
                try {

                    File file = new File(srcUrl);
                    if (file.exists()) {

                        FileReader fileReader = new FileReader(file);
                        BufferedReader bufferedReader = new BufferedReader(fileReader);

                        while ((line = bufferedReader.readLine()) != null) {
                            stringBuffer.append(line);
                            stringBuffer.append("\n");
                        }
                        fileReader.close();
                        jsondata=stringBuffer.toString();
                    }
                      
                    jsonObj = new JSONObject(jsondata);
                        String status = jsonObj.getString("Result");
                        System.out.println("JSON RESPONSE.."+status);
                        if (status.equals("success"))
                        {
                          
                             imagbin=jsonObj.getString("image-bin");
                             System.out.println("image binary.."+imagbin);
                             String imgname=jsonObj.getString("imagename");
                             System.out.println("image name.."+imgname);
                             System.out.println("READED SUCCESSFULLY..");
                           
                          

                        }
                        else
                        {
                            System.out.println("ERROR IN RATE..");
                        }
                  
                   
                  
        }          catch (Exception e) {
                        e.printStackTrace();
                    }

                 return imagbin;    
          
        }

No comments:

Post a Comment