在Android Master / Detail Flow中解析JSON

时间:2013-07-08 01:02:41

标签: android json master-detail

正如标题所说。我想使用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包中创建一个Master / Detail Flow项目。我希望我说的都是对的。如果有更多细节,请告诉我。 Android Dev非常新,但是非常感谢任何正确方向的指针。

1 个答案:

答案 0 :(得分:0)

就个人而言,我会在单独的文件中进行JSON解析,并且可能使用AsyncTask。这样您就可以解耦文件/类,因为您实际上不需要用于解析的活动。

我尝试重复使用您在上面发布的代码。话虽如此,这样的事情应该起作用或让你朝着正确的方向前进:

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();
        }
    }
}

现在,只要您想解析JSON文件并使用上面的类,您只需执行以下操作:

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

如果您有任何问题,请告诉我......