Skip to content

Instantly share code, notes, and snippets.

@D-clock
Last active July 23, 2017 04:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save D-clock/317cad34e31b06172a0fcbcd1e3a908b to your computer and use it in GitHub Desktop.
Save D-clock/317cad34e31b06172a0fcbcd1e3a908b to your computer and use it in GitHub Desktop.
Android Json 工具类
package com.clock.datatransfer;
import android.content.Context;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
/**
* Created by Clock on 2017/1/14.
*/
public class JsonUtils {
private JsonUtils() {
}
/**
* 从raw目录下读取JSON文件的信息
*
* @param context
* @param jsonFileId
* @return
*/
public static String getJsonFormRawResource(Context context, int jsonFileId) {
InputStream inputStream = context.getResources().openRawResource(jsonFileId);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
StringWriter stringWriter = new StringWriter();
byte[] buffer = new byte[1024];
String json = "";
int length = -1;
try {
while ((length = bufferedInputStream.read(buffer)) != -1) {
String bufferStr = new String(buffer, 0, length);
stringWriter.write(bufferStr);
}
stringWriter.flush();
json = stringWriter.getBuffer().toString();
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedInputStream.close();
stringWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return json;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment