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) –