2017-09-27 7 views
-1

私は資産のJSONファイルを持っていると私は私のMainActivity方法でそれを読むためにしようとしている:ログでロードJSON

private static List<JSONObject> getJSONArray(Context context) { 
    JSONArray myJSONarr=new JSONArray(); 
    try 
    { 
     AssetManager am = context.getAssets(); 
     InputStream is = am.open("chineesecardsdata.json"); 
     String resultJson = is.toString(); 
     is.close(); 
     Log.d("mainActLog","file read ok: "+resultJson); 

     try { 
     } 
     catch (JSONException e) 
     { 
     } 
    } 
    catch(
      IOException e) 
    { 
     Log.d("mainActLog","file read fail"); 
    } 
} 

は私が持っている:

ファイルには、[OK]をお読みください。 [email protected]

+2

可能な複製(https://stackoverflow.com/questions/9544737/read-file-from-assets)私は静的である方法を必要とする –

答えて

1
public String loadJSONFromAsset(Context context) { 
     String json = null; 
     try { 
      InputStream is = context.getAssets().open("yourfilename.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; 
} 

解析JSON [資産からファイルを読み込む]の

try { 
     JSONObject obj = new JSONObject(loadJSONFromAsset()); 
     JSONArray array = new JSONArray(loadJSONFromAsset()); 

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

ので、私はgetActivity – Olga

+0

使用カント私の答えを更新する場合は、私にアップしてください。 –

+0

私をupvoteしてください –

関連する問題