Question

I have a txt file which I have to get it in arraylist<hashmap<string,object>> format to use it in my code. So far I have used this code and I am getting string.

I have to add all the data to google maps. Any way to do this?

public String ReadFromfile(String fileName, Context context) {
                StringBuilder returnString = new StringBuilder();
                InputStream fIn = null;
                InputStreamReader isr = null;
                BufferedReader input = null;
                try {
                    fIn = context.getResources().getAssets()
                            .open(fileName, Context.MODE_WORLD_READABLE);
                    isr = new InputStreamReader(fIn);
                    input = new BufferedReader(isr);
                    String line = "";
                    while ((line = input.readLine()) != null) {
                        returnString.append(line);
                    }
                } catch (Exception e) {
                    e.getMessage();
                } finally {
                    try {
                        if (isr != null)
                            isr.close();
                        if (fIn != null)
                            fIn.close();
                        if (input != null)
                            input.close();
                    } catch (Exception e2) {
                        e2.getMessage();
                    }
                }
                return returnString.toString();
            }
Was it helpful?

Solution

Json:

{
"name": "Something",
"age":24
}

Java class:

class Person{
private String name;
private int age;

getter and setters
}

Download GSON jar and add it to your project.

Person p=new Gson().fromJson("{'name':'something','age':23}",Person.class);

String sterPerson=new Gson().toJson(p);

OTHER TIPS

Ultimately what you want is JSONArray. A Collection of JSONObject. JSONObject is nothing but a form of Key Value Pair.

Basically , You can store data in file in the form of JSONArray.

For Ex :

[
           {"key","value"},
           {"key","value"},
           {"key","value"},
           {"key","value"}
]

Read more about

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top