I’m reading a JSON file in my android project, just to see the first position of the array.
In fact, if this JSON is more big what normal, I think this isn’t efficient…
My json file reader:
public static JSONObject parseJSONfromInputStrem (InputStreamReader isr){
try {
BufferedReader reader = new BufferedReader(isr,8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
Where I call the function:
FileInputStream fis = c.openFileInput(file);
InputStreamReader isr = new InputStreamReader(fis);
JSONObject jso = JSONParser.parseJSONfromInputStrem(isr);
JSONArray myArray = jso.getJSONArray("data");
There are any way to read, efficiently, the first position of a JSONArray?
You should use a streaming JSON parser (think DOM vs SAX).
https://stackoverflow.com/a/823632/18573 lists some. There may be others.