写JSON文件置入资产的文件夹的Android [英] Write to file JSON into assets folder Android
问题描述
我有一个文件(ex.json)我从中取数据
{
住址:。улКурчатова,10,
注释:Взданиивокзала,на1ина2этаже
Dobavil:Сергей,
位置: {
纬度:48.474721,
东经:35.008587
},
OBJECTID:sVjaCW0JV4
}
这样
公共无效更新()
StringBuffer的SB =新的StringBuffer();
BR的BufferedReader = NULL;
尝试{
BR =新的BufferedReader(新的InputStreamReader(getAssets()开(ex.json)));
串温度;
而((TEMP = br.readLine())!= NULL)
sb.append(临时);
}赶上(IOException异常五){
e.printStackTrace();
} {最后
尝试{
br.close(); //停止阅读
}赶上(IOException异常五){
e.printStackTrace();
}
} 串myjsonstring = sb.toString();
尝试{
JSONObject的jsonObjMain =新的JSONObject(myjsonstring);
JSONArray jsonArray = jsonObjMain.getJSONArray(结果);
的for(int i = 0; I< jsonArray.length();我++){
JSONObject的jsonObj = jsonArray.getJSONObject(I)
字符串ADRES = jsonObj.getString(住址);
字符串评论= jsonObj.getString(评论);
串dobavil = jsonObj.getString(Dobavil);
JSONObject的C = jsonArray.getJSONObject(I)
JSONObject的位置= c.getJSONObject(位置);
字符串经纬度= location.getString(纬度);
串经度= location.getString(经度);
} }赶上(JSONException E){
e.printStackTrace();
}
}
我想知道你是否可以将数据添加到文件?
我怎样写一个字符串和位置的数组?
这可能是更容易的文本文件办呢?
我想知道你是否可以将数据添加到文件?
块引用>资产是只读的在运行时。欢迎您将数据写入到内部存储(例如,
getFilesDir()
)。当你走在读取数据,请检查您是否有内部存储修改后的副本,如果它存在使用它。否则,回落到加载从资产的数据,因为你是presently做的。
我怎样写一个字符串和位置的数组?
块引用>使用
的JSONObject
或JsonWriter
。或者,EN code在其他格式的数据(例如,XML,CSV)。或者,使用一个数据库。I have a file (ex.json) from which I take the data
{ "Adres": "ул. Курчатова, 10", "Comment": "В здании вокзала, на 1 и на 2 этаже", "Dobavil": "Сергей", "location": { "latitude": 48.474721, "longitude": 35.008587 }, "objectId": "sVjaCW0JV4" }
doing so
public void update() StringBuffer sb = new StringBuffer(); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(getAssets().open("ex.json"))); String temp; while ((temp = br.readLine()) != null) sb.append(temp); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); // stop reading } catch (IOException e) { e.printStackTrace(); } } String myjsonstring = sb.toString(); try { JSONObject jsonObjMain = new JSONObject(myjsonstring); JSONArray jsonArray = jsonObjMain.getJSONArray("results"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); String adres = jsonObj.getString("Adres"); String comment = jsonObj.getString("Comment"); String dobavil = jsonObj.getString("Dobavil"); JSONObject c = jsonArray.getJSONObject(i); JSONObject location = c.getJSONObject("location"); String lat = location.getString("latitude"); String lon = location.getString("longitude"); } } catch (JSONException e) { e.printStackTrace(); } }
I want to know whether you can append data to the file? How do I write a string and an array of location? It might be easier to do it with a text file?
解决方案I want to know whether you can append data to the file?
Assets are read-only at runtime. You are welcome to write your data to internal storage (e.g.,
getFilesDir()
). When you go to read in the data, check to see if you have a modified copy in internal storage, and use it if it exists. Otherwise, fall back to loading the data from assets, as you are presently doing.How do I write a string and an array of location?
Use
JSONObject
orJsonWriter
. Or, encode the data in some other format (e.g., XML, CSV). Or, use a database.这篇关于写JSON文件置入资产的文件夹的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!