how to load local json file in my project?   

1 806 Question In | Android App
Sat Feb 2, 2019 Abhishek

i want to create local json file in my android project
						

add json file in assets folder of your project.

public class MainActivity extends AppCompatActivity {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       

        try {

            JSONArray jsonArray = new JSONArray(getJson(this, "temp.json"));


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

public String getJson(Context context, String localJsonFileName) {
        String json = null;
        try {
            InputStream is = context.getAssets().open(localJsonFileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            Toast.makeText(context, ex.toString(), Toast.LENGTH_SHORT).show();
            return null;
        }
        return json;
    }
}

Admin

13  Sat Feb 2,2019  Solved      


Subscribe & Get Regular Updates On Your E-mail