0
我創建了一個JSON文件,並且我上傳到了一個域中,我的目標是解析該文檔並獲取信息,但是我做錯了,這是我的JSON文件:關於解析JSON和顯示結果的幫助(Android)
{
"Show" : {
"id" : "abcde123",
"name" : "Traviata",
"date" : "September 15, 2011"
}
}
這是我用它來解析JSON的Java代碼:
private void parse(){
try {
URL eventsJSON = new URL("http://www.site.com/test/sample.json");
URLConnection tc = eventsJSON.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONArray jArray = new JSONArray(line);
for (int i = 0; i < jArray.length(); i++) {
eventsJSONObject = (JSONObject) jArray.get(i);
}
}
JSONArray eventsArray = eventsJSONObject.getJSONArray("Show");
//name of the show
Log.d("LOG", jArray.getJSONObject(0).getString("name").toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
但該類不工作..有人可以幫助我嗎? 謝謝! :)
編輯: 這是一個工作的代碼,但我不能讓它工作從文件
String jString = "{\"Show\" : {\"from\" : \"June 12, 2011\",\"id\" : \"abcde123\",\"name\" : \"Traviata\"}}";
JSONObject jObject = new JSONObject(jString);
JSONObject eventsObject = jObject.getJSONObject("Shows");
String eventId = eventsObject.getString("id");
String eventName = eventsObject.getString("name");
String eventDateFrom = eventsObject.getString("from");
解析JSON如何執行緩衝的讀者和記憶JSON代碼在jString字符串?
感謝您的回答,但你可以請嘗試編輯我的代碼?我仍然無法使它工作 –
編輯不會真正幫助你,相反 - 一個(非常)簡短而漂亮的教程(查看JSON結構以及它們如何獲取每個字段):http://www.androidcompetencycenter。 com/2009/10/json-parsing-in-android/ – MByD
很酷,謝謝!該教程工作正常,但問題是,字符串行始終爲空......我誤寫了while循環嗎? –