0

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?

1 Answer 1

2

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 or JsonWriter. Or, encode the data in some other format (e.g., XML, CSV). Or, use a database.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick reply. I am a novice programmer. Could you give an example of such a solution?
@Chernoff20: Sorry, I do not really have a simple example of this solution handy.
One of the most common practices is to write your JSON data as String inside SharedPreferences. But it depends on the data volume, if this JSON is going to be giant is better to write in other ways

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.