Android 解析assets目录下的json文件

来源:互联网 发布:旁氏米粹怎么样知乎 编辑:程序博客网 时间:2024/06/06 20:27
Context test_Context = null;
        String test_package = packageName;
            try
            {            
                test_Context = context.createPackageContext(
                        test_package, Context.CONTEXT_IGNORE_SECURITY);
                AssetManager s =  test_Context.getAssets();
                try{
                    InputStream is = s.open("info.json");
                    byte [] buffer = new byte[is.available()] ;
                    is.read(buffer);  
                    String json = new String(buffer,"utf-8");
                    is.close();
                    JSONObject obj;
                    try {
                        obj = new JSONObject(json);
                        String title = obj.getString("name");
                        if(title.isEmpty() == false){
                            return title;
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }  
                    
                }catch(IOException e){
                    e.printStackTrace();
                }
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }