1

This method reads text from json file.I want to read only name,date,time from json file.How can I accomplish this.My json file contains various records each record consisting of name,lat,lon,image_name,date,time.I need to read name,date,time of one record and place it as radio button1 value then read second name,date,time of second record and place it in radio butto2 value. I also need to delete specific name and update specific name. Please help me and Thank You in advance.Have a great day ahead.

 String root = Environment.getExternalStorageDirectory().toString(); //get access to directory path
            File myDir = new File(root + "/GeoPark");//create folder in internal storage
            myDir.mkdirs();// make directory
            File file = new File(myDir, FILENAME);//making a new file in the folder

            if(file.exists())   // check if file exist
            {
                //Read text from file
                StringBuilder text = new StringBuilder();

                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String line;

                    while ((line = br.readLine()) != null) {
                        text.append(line);
                    }
                }
                catch (IOException e) {
                    //You'll need to add proper error handling here
                }
                //Set the text
                String x=text.toString();
                String z=x.replace("{","").replace("date:","").replace("time:","").replace("Record:","").replace("[","").replace("latitude:","").replace("longitude:","").replace("name:","").replace("address:","").replace("pin:","").replace("area:","").replace("image:","").replace("\"","").replace("]","").replace("}","");
                String[] y=z.split(",");

                rb1.setText(y[3].toString()+","+y[7].toString()+","+y[8].toString());

            }
            else
            {
                rb1.setText("Sorry file doesn't exist!!");
            }

This is my json file

{Record:["lat":"22.5835","lon":"88.456","name":"aa","add":"cc",date:30/04/2018,time:21:05:10]}
{Record:["lat":"22.583544","lon":"88.45642","name":"BB","add":"cc",date:30/04/2018,time:21:05:40]}
Manu
  • 11
  • 6

3 Answers3

0

Consider using GSON.

Serialization is as simple as calling

String jsonStr = Gson().toJson(obj)

And for deserialization,

MyClass deserialized = Gson().fromJson(jsonStr, MyClass.class);

JSON on Android - serialization

drone6502
  • 433
  • 2
  • 7
  • Can you please help me with the code I provided.Changes in that code will be helpful – Manu Apr 30 '18 at 16:21
0

First you should convert your string to json object then parse it as you want.

Assuming your json is

String data="{'foo':'bar','coolness':2.0, 'altitude':39000, 'pilot':{'firstName':'Buzz','lastName':'Aldrin'}, 'mission':'apollo 11'}";

then you can easily do

JSONObject json = (JSONObject) JSONSerializer.toJSON(data);        
    double coolness = json.getDouble( "coolness" );
    int altitude = json.getInt( "altitude" );
    JSONObject pilot = json.getJSONObject("pilot");
    String firstName = pilot.getString("firstName");
    String lastName = pilot.getString("lastName");

    System.out.println( "Coolness: " + coolness );
    System.out.println( "Altitude: " + altitude );
    System.out.println( "Pilot: " + lastName );

Isn't it easy?

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Can you please help me with the code I provided.Changes in that code will be helpful – Manu Apr 30 '18 at 16:21
0

Here, try this

String root = Environment.getExternalStorageDirectory().toString(); //get access to directory path
            File myDir = new File(root + "/GeoPark");//create folder in internal storage
            myDir.mkdirs();// make directory
            File file = new File(myDir, FILENAME);//making a new file in the folder

            if(file.exists())   // check if file exist
            {
                //Read text from file
                StringBuilder text = new StringBuilder();

                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String line;

                    while ((line = br.readLine()) != null) {
                        text.append(line);
                    }
                }
                catch (IOException e) {
                    //You'll need to add proper error handling here
                }
                //Set the text


            JSONObject obj = new JSONObject(text.toString());
            JSONArray arr = obj.getJSONArray("Record");
            for (int i = 0; i < arr.length(); i++){
                 String name = arr.getJSONObject(i).getString("name");
                 String date = arr.getJSONObject(i).getString("date");
                 String time = arr.getJSONObject(i).getString("time");
              }
            }
            else
            {
                rb1.setText("Sorry file doesn't exist!!");
            }

you can also see -> http://theoryapp.com/parse-json-in-java/

Deepak kaku
  • 1,218
  • 9
  • 15