0
我目前正在開發一個應用程序,需要從一個未命名的數組中解析JSON對象。
我只能設法解析JSON數組,名稱如下:http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt。從JSON數組解析JSON對象
高於予用於所述一個代碼是
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String asd = buffer.toString();
JSONObject parentObject = new JSONObject(asd);
JSONArray parentArray = parentObject.getJSONArray("movies");
JSONObject fObject = parentArray.getJSONObject(0);
String movie = fObject.getString("movie");
int year = fObject.getInt("year");
return movie + year;
代碼包括「電影」,這是陣列名稱。
我應該更改哪些內容才能解析JSON數組中的對象,如https://restcountries.eu/rest/v1/all?
您應該最好從'doInBackground'返回'buffer.toString()'或'parentObject'。在'onPostExecute'中執行剩餘的JSON解析。 –
[如何在Android中解析JSON]可能的重複(http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) –