get JSON Reader - Android File Input Output

Android examples for File Input Output:Json

Description

get JSON Reader

Demo Code


//package com.java2s;
import android.util.JsonReader;

import java.io.ByteArrayInputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

public class Main {
    public static JsonReader getReader(byte[] bytes) {
        ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
        return new JsonReader(new InputStreamReader(stream));
    }/*from  w  w w. j a va  2s  .  co  m*/

    public static JsonReader getReader(String text) {
        if (text == null)
            return null;

        // Get JsonReader from String
        byte[] bytes;
        try {
            bytes = text.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ignored) {
            return null;
        }
        ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
        return new JsonReader(new InputStreamReader(stream));
    }
}

Related Tutorials