load Json String from Asset - Android App

Android examples for App:Assets String

Description

load Json String from Asset

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;

public class Main {
    public static String loadJson(String name, Context context)
            throws IOException {

        StringBuilder sb = new StringBuilder();
        sb.append("db").append("/");
        sb.append(name);/*  w  w w. j  a v  a2  s. com*/
        sb.append(".json");

        String fileName = sb.toString();
        InputStream is = context.getAssets().open(fileName);

        return makeContent(is);
    }

    private static String makeContent(InputStream is) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        return sb.toString();
    }
}

Related Tutorials