Добрый вечер, получил json с внешнего ресурса, теперь хочу достать значение из "rates"(в идеале положить rates в Map), как это сделать? Спасибо.
JSON файл
{"base":"EUR","date":"2017-09-26","rates":{"AUD":1.4944,"BGN":1.9558,"BRL":3.7299,"CAD":1.4605,"CHF":1.1452,"CNY":7.8189,"CZK":26.056,"DKK":7.4404,"GBP":0.87775,"HKD":9.2083,"HRK":7.493,"HUF":310.77,"IDR":15785.0,"ILS":4.1579,"INR":77.194,"JPY":131.99,"KRW":1341.6,"MXN":21.182,"MYR":4.9662,"NOK":9.2843,"NZD":1.6369,"PHP":60.018,"PLN":4.2824,"RON":4.6013,"RUB":67.987,"SEK":9.5473,"SGD":1.5968,"THB":39.139,"TRY":4.1739,"USD":1.1787,"ZAR":15.733}}
Java код
package vadimkonyuh.test;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
public static String LOG_TAG = "my_log";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ParseTask().execute();
}
private class ParseTask extends AsyncTask<Void, Void, String> {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String resultJson = "";
@Override
protected String doInBackground(Void... params) {
// получаем данные с внешнего ресурса
try {
URL url = new URL("http://api.fixer.io/latest");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resultJson = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return resultJson;
}
@Override
protected void onPostExecute(String strJson) {
super.onPostExecute(strJson);
// выводим целиком полученную json-строку
Log.d(LOG_TAG, strJson);
try {
JSONObject
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}