0

As the title says. I'd like to just use a JSON. Here is my code for what I'm guessing is my main activity. This grabs all the contents and places them in their respectful variables I'm hoping. Placed at the beginning:

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

Now I have another file called WordContent.java that defines these variable again (a non edited version):

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

I haven't edited them yet because I have no idea where to go from here. Or rather how to put the contents of my JSON to the WordItem so they show up when I run the program. Another way to look at all of my code that is similar to this is to just create a Master/Detail Flow project in the Eclipse ADT bundle. I hope I'm saying all of this right. Let me know if there are more details I should shed. Very new to Android Dev but any pointer in the right direction is greatly appreciated.

4

1 回答 1

0

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

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

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

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

于 2013-07-08T03:27:28.057 回答