将文件从安卓中的资产文件夹转换为arraylist<hashmap<string,object>>



我有一个txt文件,我必须以arraylist<hashmap<string,object>>格式获取它才能在我的代码中使用它。到目前为止,我已经使用了这段代码,并且正在获得字符串。

我必须将所有数据添加到谷歌地图。有什么办法可以做到这一点吗?

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();
            }

Json:

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

爪哇类:

class Person{
private String name;
private int age;
getter and setters
}

下载 GSON jar 并将其添加到您的项目中。

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

最终你想要的是JSONArray。JSONObject的集合。JSONObject只不过是键值对的一种形式。

基本上,你可以以JSONArray的形式将数据存储在文件中。

例如:

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

阅读更多 关于

  • JSONArray : http://www.json.org/javadoc/org/json/JSONArray.html
  • JSONObject : http://www.json.org/javadoc/org/json/JSONObject.html

最新更新