How To Fetch Data From Local Json File?
Solution 1:
Yes, it is possible. Next time provide some code so we have something to work with, there is not much people can do to solve this issue.
To get you started look at this tutorial about JSON: http://www.vogella.com/tutorials/AndroidJSON/article.html
This question will help you read a file locally: How can I read a text file in Android?
When you got the file as a string you can create a JSONObject
and parse the URLs and retrieve the images.
Solution 2:
Have you tried using the Gson library for Android ?
I'm using it and it works perfectly, you'll create objects that represent perfectly the Json. You then apply the Gson method on the Json and it will automatically fetch all the data in the object / objects that you created.
If you want to have a tutorial about how to use Gson, I think this guy explains really well. I suggest you to try this technic before asking anything else as it is a really commonly used library to parse Json Data. after using it, you'll have objects that reflect exactly the content of your Json File !
Hope it helped !
Oh btw, here is a website that will help you farmat your Json. I found it pretty useful myself, hope you'll like it ;)
Solution 3:
Well its quite simple! here it goes:
privatevoidDoWork() {
Filefile=newFile(Environment.getExternalStorageDirectory().getAbsolutePath(),
"file.json"); //path to your file assuming its in root of SD cardStringcontent="";
try {
BufferedReaderbr=newBufferedReader(newFileReader(file));
try {
StringBuildersb=newStringBuilder();
Stringline= br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
content = sb.toString();
} finally {
br.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
mTextView.setText(""); // A multiline text view big enough
mTextView.append("\n\n-----START CONTENT-----\n\n");
mTextView.append(content);
mTextView.append("\n\n-----START JSON-----\n\n");
try {
JSONArrayjsonArray=newJSONArray(content);
intlength= jsonArray.length();
for (inti=0; i < length; i++) { /itterate throu json array
JSONObjectobj= jsonArray.getJSONObject(i);
//here you can access individual parts of you json object by getString()...getBoolean().... etc...
mTextView.append("\n" + obj.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
To read an internal file belonging to the app directory try:
this.openFileInput("yourfile.txt");
//`this` is the reference to you Activity or Context
Example:
FileInputStreamin= openFileInput("filename.txt");
InputStreamReaderinputStreamReader=newInputStreamReader(in);
BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);
StringBuildersb=newStringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
more details here
Solution 4:
here is code read JSON
data from file that file keep in asset folder in project
publicJSONObjectreadDummyResponse() {
InputStream inputStream = null;
try {
inputStream = this.getAssets().open("sample_response.txt");
} catch (IOException e1) {
e1.printStackTrace();
}
JSONObject jsonObject = null;
if (inputStream != null) {
BufferedReader reader = newBufferedReader(newInputStreamReader(
inputStream));
StringBuffer buffer = newStringBuffer();
String statement;
try {
while ((statement = reader.readLine()) != null) {
if (statement.trim().length() > 0) {
buffer.append(statement);
}
}
} catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
if (buffer.length() > 0) {
try {
jsonObject = newJSONObject(buffer.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return (JSONObject) jsonObject;
}
Post a Comment for "How To Fetch Data From Local Json File?"