load JSON From Asset - Android File Input Output

Android examples for File Input Output:Json

Description

load JSON From Asset

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;

public class Main {
    public static String loadJSONFromAsset(Context context,
            String jsonFileName) {
        String json = null;//w  w w  . j  a  va2s . co  m
        try {

            InputStream is = context.getAssets().open(
                    jsonFileName + ".json");

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

            json = new String(buffer, "UTF-8");

        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }
}

Related Tutorials