第一步:在android studio中添加json文件;
文章来源:https://www.toymoban.com/news/detail-561083.html
第二步:读取文件的函数文章来源地址https://www.toymoban.com/news/detail-561083.html
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;
}
到了这里,关于android studio 添加并读取json配置文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!