2016-03-03 31 views
0

我有JSON文件:资产文件夹 “document.json”JSON对象from.json文件

document.json:

{ 
    "string": "Hello World" 
} 

我能做到这一点从JSON获得Android的字符串文件?

JSONObject jresponse = new JSONObject(R.assets.document); 
string response = jresponse.getString("string"); 

我做错了什么或它是正确的?

有没有什么方法可以传递.json文件并获取所需的字符串?

+0

的JSONObject不采取R.资产资源...是不是一个整数? –

+3

你在这里:http://stackoverflow.com/questions/19945411/android-java-how-can-i-parse-a-local-json-file-from-assets-folder-into-a-listvi –

+0

As “这是正确的吗?”好吧,它可能不会编译... –

回答

0

不行,你必须从第一

InputStream is = getActivity().getAssets().open("document.json"); 
     int size = is.available(); 
     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 
     String myJson = new String(buffer, "UTF-8"); 

然后资产阅读JSON您可以分析

JSONObject obj = new JSONObject(myJson); 
String myFinalString = obj.getString("") 

For further help