2016-01-21 107 views
-4

JSON文件我有JSON文件,我需要类文件关于它如何读取从资产文件夹中的Android

{ 
    "CountryDetail":[ 
    { 
     "C_country":"India", 
     "C_sunrise":1381107633, 
     "C_sunset":1381149604 
    }, 
    "weatherDetail": 
    { 
     "w_id":711, 
     "w_main":"Smoke", 
     "w_description":"smoke", 
    } 
, 
"Tempdetail": 
    { 
     "t_temp":304.15, 
     "t_pressure":1009, 
    } 

} 
+0

参考http://stackoverflow.com/questions/19945411/android-java-how-can-i-parse-a-local-json-file-from-assets-folder-into- a-listvi – sasikumar

+2

发布有效的json。 –

回答

10

试试这个,你会得到该文件的JSONObject的。(JSON应该是有效的)。 您可以通过clicking here

JSONObject obj = new JSONObject(readJSONFromAsset()); 

public String readJSONFromAsset() { 
    String json = null; 
    try { 
     InputStream is = getAssets().open("yourFile.json"); 
     int size = is.available(); 
     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 
     json = new String(buffer, "UTF-8"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
     return null; 
    } 
    return json; 
} 
+0

使用此我也面临一些错误 –

+0

可能是因为你的JSON无效。用jsonlint.com检查json –

+0

JSONObject obj = new JSONObject(readJSONFromAsset()); //这里我面对错误 \t public String readJSONFromAsset(){ \t String json = null; \t try { \t InputStream is = getAssets()。open(“CountryDertail.json”); \t int size = is.available(); \t byte [] buffer = new byte [size]; \t is.read(buffer); \t is.close(); json = new String(buffer,“UTF-8”); (IOException,ex){ \t} catch(IOException ex){ \t ex.printStackTrace(); \t return null; \t} \t return json; \t} –

1

检查JSON来在干净的方式读取资产JSON对象。

@RequiresApi(api = Build.VERSION_CODES.KITKAT) 
public JsonObject getJSONResource(Context context) { 
    try (InputStream is = context.getAssets().open("resource.json")) { 
     JsonParser parser = new JsonParser(); 
     return parser.parse(new InputStreamReader(is)).getAsJsonObject(); 
    } catch (Exception e) { 
     Log.e(TAG, e.getMessage(), e); 
    } 
    return null; 
} 

为什么有一个API inlcusion的原因,是由于,在异常处理使用与resources.Currently尝试我用简单的JSON,考虑GSON的事实。

0

您的JSON是无效 错误是:

Error: Parse error on line 7: 
...},  "weatherDetail": {   "w_id": 711, 
----------------------^ 
Expecting 'EOF', '}', ',', ']', got ':' 
如果你想验证你的JSON去这个网站

https://jsonlint.com/

0

我做这件事情存储从JSON县名单本地数据库,这是我的代码:

public String loadJSONFromAsset() { 
    String json = null; 
    try { 

     InputStream is = getAssets().open("countries.json"); 

     int size = is.available(); 

     byte[] buffer = new byte[size]; 

     is.read(buffer); 

     is.close(); 

     json = new String(buffer, "UTF-8"); 


    } catch (IOException ex) { 
     ex.printStackTrace(); 
     return null; 
    } 
    return json; 

} 

    try { 
      JSONObject obj = new JSONObject(loadJSONFromAsset()); 
      JSONArray countries=obj.getJSONArray("countries"); 
      for (int i=0;i<countries.length();i++){ 
       JSONObject jsonObject=countries.getJSONObject(i); 
       String code=jsonObject.getString("code"); 
       String nameAr=jsonObject.getString("nameAr"); 
       String nameEn=jsonObject.getString("nameEn"); 
       countryList.add(new Country(code,nameAr,nameEn)); 
      } 
      DBHelper.getInstance(activity).insertCountries(countryList); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

and this my j儿子格式:

{"countries": [ 
{ 
    "nameEn": "Afghanistan", 
    "code": "AF", 
    "nameAr": "أفغانستان" 
}, 
{ 
    "nameEn": "Åland Islands", 
    "code": "AX", 
    "nameAr": "جزر أولان" 
}]} 
相关问题