2016-01-21 149 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": "جزر أولان" 
}]} 
相關問題