第一步:在android studio中添加json文件;
第二步:读取文件的函数
private String[] getJosnData(){
String result[] = null;
List<String> list = new ArrayList<>();
try {
//获取本地的Json文件
AssetManager assetManager = mContext.getAssets();
InputStream open = assetManager.open("vendor.json");
InputStreamReader isr = new InputStreamReader(open, "UTF-8");
//包装字符流,将字符流放入缓存里
BufferedReader br = new BufferedReader(isr);
String line;
//StringBuilder和StringBuffer功能类似,存储字符串
StringBuilder builder = new StringBuilder();
while ((line = br.readLine()) != null) {
//append 被选元素的结尾(仍然在内部)插入指定内容,缓存的内容依次存放到builder中
builder.append(line);
}
br.close();
isr.close();
//builder.toString() 返回表示此序列中数据的字符串
//使用Json解析
JSONObject jsonObject = new JSONObject(builder.toString());
org.json.JSONObject json = new org.json.JSONObject(builder.toString());
String data = json.getString("vendor");
org.json.JSONArray array = new org.json.JSONArray(data);
for (int i=0, j=0; i < array.length(); i++) {
org.json.JSONObject iter= array.getJSONObject(i);
Iterator keys = iter.keys();
while(keys.hasNext()) {
String key = (String) keys.next();
String value = iter.getString(key);
System.out.println("abcdef :" + key + " : " + value);
list.add(key);
}
}
} catch (JSONException | IOException e) {
e.printStackTrace();
}
result = new String[list.size()];
for( int i=0; i<list.size(); ++i){
result[i] = list.get(i);
System.out.println("abcdefg : " + result[i]);
}
return result;
}