2013-07-08 43 views
0

正如標題所示。我想只使用JSON。這是我猜測我的主要活動的代碼。這抓住了所有的內容,並把它們放在我期望的尊重變量中。開頭放置:解析Android中的JSON主/細節流程

public class WordDetailActivity extends FragmentActivity { 

// Reading text file from assets folder 
StringBuffer sb = new StringBuffer(); 
BufferedReader br = null; { 

try { 
br = new BufferedReader(new InputStreamReader(getAssets().open(
"wordlist.txt"))); 
String temp; 
while ((temp = br.readLine()) != null) 
sb.append(temp); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
try { 
br.close(); // stop reading 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 

String myjsonstring = sb.toString(); 


// Try to parse JSON 
try { 
// Creating JSONObject from String 
JSONObject jsonObjMain = new JSONObject(myjsonstring); 

// Creating JSONArray from JSONObject 
JSONArray jsonArray = jsonObjMain.getJSONArray("employee"); 

// JSONArray has four JSONObject 
for (int i = 0; i < jsonArray.length(); i++) { 

// Creating JSONObject from JSONArray 
JSONObject jsonObj = jsonArray.getJSONObject(i); 

// Getting data from individual JSONObject 
int id = jsonObj.getInt("id"); 
String word = jsonObj.getString("word"); 
String dictionary = jsonObj.getString("dictionary"); 

} 

} catch (JSONException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
}} 

現在我已經叫WordContent.java另一個文件中再次定義這些變量(非編輯的版本):

public static Map<String, WordItem> ITEM_MAP = new HashMap<String, WordItem>(); 

static { 
    // Add 3 sample items. 
    addItem(new WordItem("1", "This Word", "Blah blah blah")); 

} 

private static void addItem(WordItem item) { 
    ITEMS.add(item); 
    ITEM_MAP.put(item.id, item); 
} 

/** 
* A dummy item representing a piece of content. 
*/ 
public static class WordItem { 
    public String id; 
    public String word; 
    public String dictionary; 

    public WordItem(String id, String word, String dictionary) { 
     this.id = id; 
     this.word = word; 
     this.dictionary = dictionary; 
    } 

    @Override 
    public String toString() { 
     return word; 
    } 
} 
} 

我還沒有修改他們還因爲我有不知道該從哪裏出發。或者更確切地說,如何將我的JSON的內容添加到WordItem中,以便在運行程序時顯示它們。查看與此類似的所有代碼的另一種方法是在Eclipse ADT捆綁包中創建一個主/細節流項目。我希望我說的都是對的。請告訴我是否應該提供更多細節。非常新的Android開發,但任何指針在正確的方向非常感謝。

回答

0

就我個人而言,我會在單獨的文件中執行JSON解析,並可能使用AsyncTask。這樣你就可以解耦你的文件/類,因爲你真的不需要解析的Activity。

我試着重複使用上面發佈的代碼。有了這樣說,這樣的事情應該工作,或者把你在正確的方向:每當你想解析JSON文件,並使用上面你根本類以下

public class ParseJsonTask extends AsyncTask<Void, Void, String> { 

    private Context mCtx; 

    public ParseJsonTask(Context ctx) { 
     this.mCtx = ctx; 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     // Reading text file from assets folder 
     StringBuffer sb = new StringBuffer(); 
     BufferedReader br = null; 

     try { 
      br = new BufferedReader(new InputStreamReader(mCtx.getAssets().open("wordlist.txt"))); 
      String temp; 
      while ((temp = br.readLine()) != null) 
       sb.append(temp); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       br.close(); // stop reading 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.tostring(); 
    } 

    @Override 
    protected void onPostExecute(Void jsonString) { 
     WordContent word = new WordContent(); // We use this oject to add the JSON data to WordItem 
     // Try to parse JSON 
     try { 
      // Creating JSONObject from String 
      JSONObject jsonObjMain = new JSONObject(jsonString); 
      // Creating JSONArray from JSONObject 
      JSONArray jsonArray = jsonObjMain.getJSONArray("employee"); 
      // JSONArray has four JSONObject 
      for (int i = 0; i < jsonArray.length(); i++) { 
       // Creating JSONObject from JSONArray 
       JSONObject jsonObj = jsonArray.getJSONObject(i); 
       // Getting data from individual JSONObject 
       int id = jsonObj.getInt("id"); 
       String word = jsonObj.getString("word"); 
       String dictionary = jsonObj.getString("dictionary"); 
       // We can use the three variables above... 
       word.addItem(new WordItem(id, word, dictionary)); 
       // or we can simply do... 
       // word.addItem(new WordItem(jsonObj.getInt("id"), jsonObj.getString("word"), jsonObj.getString("dictionary"))); 
      } 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

現在:

ParseJsonTask task = new ParseJsonTask(getBaseContext()); 
task.execute(); 

讓我知道如果您有任何疑問...

+0

我只是想添加在你的WordItem類中你有一個字符串類型的id。你將會想要將你的id從一個int轉換爲一個String。使用.toString()方法應該可以工作。 – michaelcarrano

+0

首先謝謝你的回答。這絕對是一種正確的方向。我在理解如何創建上下文時遇到問題,因此我可以在活動之外使用getAssets()方法。我收到的另一個錯誤是「ParseJsonTask類型的onPostExecute(Void)方法必須覆蓋或實現一個超類型方法」我不完全確定這意味着什麼。 – joshuar500

+0

我更新了我的答案,並做了一些更正。您現在將看到ParseJsonTask的構造函數,它將Context作爲參數。我相信在getBaseContext()傳遞應該工作,如果沒有,然後嘗試getActivity()。 – michaelcarrano