2015-08-25 53 views
0

我有一個應用程序,我得到一個json並讀出它。 爲了測試我的應用程序hava中包含json的文件。 問題是我不能像在C#中那樣從我的電腦中獲取它。 因此,我創建了一個資產文件夾,並將其中的文件。 現在我有這樣的代碼:如何從android工作室的文本文件中獲取json

BufferedReader r = null; 
AssetManager a = getAssets(); 
StringBuilder s = new StringBuilder(); 
try{ 
r = new BufferedReader(new InputStreamReader(a.open("My.json"), "UTF-8")); 
String l; 
while((l = r.readLine()) != null){ 
s.append(l); 
} 

JSONObject g = new JSONObject(s.toString()); 

每次我運行此,並獲得了while循環debuger說,沒有框架可用。我在循環中和之後都有斷點,即使等待很長時間,它們也不會受到影響。 我在做什麼錯了還是有其他更好的方式從我的文本文件中獲取jsonObject?

+0

顯示您的Json文件 –

回答

0

不是把JSON文件中的資產的文件夾res目錄下創建原始文件夾,將在原文件夾中的JSON文件。然後嘗試在活動

這段代碼活動中創建一個函數從這樣的文件中獲取字符串..

 public String readTextFile(InputStream inputStream) { 
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 

     byte buf[] = new byte[1024]; 
     int len; 
     try { 
      while ((len = inputStream.read(buf)) != -1) { 
       outputStream.write(buf, 0, len); 
      } 
      outputStream.close(); 
      inputStream.close(); 
     } catch (IOException e) { 

     } 
     return outputStream.toString(); 
    } 

菲娜LLY在OnCreate中選擇原始文件夾中的文件和傳遞的InputStream函數從file..like此得到串...

try { 
     //here my json file is jsondata.json which is in raw folder. 
     InputStream fileStream=getResources().openRawResource(R.raw.jsondata); 
     String sxml = readTextFile(fileStream); 
     //here converting the string as json object 
    JSONObject json = new JSONObject(sxml); 
    TextView text=(TextView)findViewById(R.id.datedemo); 
    //here convert the json as string and displayed in textview 
    text.setText(json.toString()); 

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

感謝您的工作近乎完美。我不能創建我的json文件,但字符串是sxml是正確的,所以我想我的json太大或者其他的東西是錯的。 – HHHH

+0

@HHHH首先在記事本中創建json,然後將其保存爲filename.json,然後將該文件粘貼到原始文件夾中。在將.json文件放入原始文件夾之前,請確保文件中的json有效或不使用此url.https:// jsonformatter .curiousconcept.com/ – Vishwa

+0

感謝您的鏈接,我不知道這一點。它說我的JSON執行了最大數量的字符。你知道是否有比JsonObject更大的東西? – HHHH

0

你可以做這樣的事情(爲的JSONObject): 其中dbname =文件名

public JSONArray getData(Activity act, String dbName) { 
    String jsonString = null; 
    InputStream inputStream = null; 
    JSONArray data = null; 

    if (act.getFileStreamPath(dbName).exists()) { 
     data = new JSONArray(); 

     try { 
      inputStream = act.openFileInput(dbName); 
      BufferedInputStream bis = new BufferedInputStream(inputStream); 
      ByteArrayBuffer baf = new ByteArrayBuffer(20); 

      int current = 0; 
      while ((current = bis.read()) != -1) { 
       baf.append((byte) current); 
      } 
      jsonString = new String(baf.toByteArray()); 
      data = new JSONArray(jsonString); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return data; 
} 
0

使用下面的代碼。

try { 
    String mFileContent = loadFileContent(); 
    JSONObject mJsonObj = new JSONObject(mFileContent); 

    } catch (Throwable t) { 
     Log.d("TAG", "Could not parse malformed JSON: \n"+ mFileContent); 
    } 


    private String loadFileContent() 
    { 
      String mFilePath = "yourFilePath"; 
      FileInputStream fos = null; 
      String fileContent = ""; 
      try { 
       fos = new FileInputStream(mFilePath); 
       int fileSize = fos.available(); 
       byte[] bytes = new byte[(int)fileSize]; 
       int offset = 0; 
       int count=0; 
       while (offset < fileSize) { 
        count=fos.read(bytes, offset, fileSize-offset); 
        if (count >= 0) 
         offset += count; 
       } 
       fos.close(); 

       if(fileSize == 0) 
        return null; 

       fileContent = new String(fileContent, "UTF-8"); 

      } catch (Exception e) { 
       return null; 
      } 

      return fileContent; 
    } 
相關問題