Last active
July 23, 2017 04:59
-
-
Save D-clock/317cad34e31b06172a0fcbcd1e3a908b to your computer and use it in GitHub Desktop.
Android Json 工具类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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