Webサーバーから特定のJSONデータを取得しようとしていますが、これをアンドロイドアプリに特定の値を表示するために使用したいと考えています。要求は成功します(データがサーバーから取得された場合など)。実際にJSONを使用する場合は、エラーorg.json.JSONObject cannot be converted to JSONArray
が返されます。 590b14eb66b8f3786317e571
をリクエストごとに変更することができるのMongoDB文書のIDであり、この例ではAndroid:org.json.JSONObjectをJSONArrayに変換できません
{
"590b14eb66b8f3786317e571": {
"updatedAt": "2017-05-04T11:47:55.000Z",
"created_at": "2017-05-04T11:47:55.000Z",
"UId": "590b10981da091b2618a4914",
"value": 37,
"_id": "590b14eb66b8f3786317e571",
"__v": 0
}
}
:検索JSONは以下の構造を有します。だから私はそれを使用してJSON配列から必要な要素を取得することはできません。私が興味を持っているのは "価値"です。
コードデータ取得するために:その後、必要な実際のキー/値を取得するには
private JSONArray getSingleTemperature(URL url) throws IOException, JSONException {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String forecastJsonString = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("charset", "UTF-8");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if(inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if(buffer.length() == 0) {
return null;
}
forecastJsonString = buffer.toString();
Log.i("JSON", forecastJsonString);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(urlConnection != null) {
urlConnection.disconnect();
}
if(reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceHolderFragment", "Error closing stream", e);
}
}
}
JSONArray ja = new JSONArray(forecastJsonString);
return ja;
}
を、私はこのコードを使用しようとしている:
@Override
public void processFinish(JSONArray json) {
try {
if(json != null) {
JSONObject jo = json.getJSONObject(0);
String temp = jo.getString("value");
temperature = Integer.parseInt(temp);
temperatureView.setText(temp + "°C");
checkTemperature();
} else {
buildAlertDialog(getResources().getString(R.string.server_error_title), getResources().getString(R.string.server_error_message));
}
} catch(JSONException e) {
e.printStackTrace();
}
}
私はすでに他の質問を通して見てきました(thisと同じですが)most solutions seem to be build on knowing the name of the root element(この例では590b14eb66b8f3786317e571
)ですが、私の場合はこの名前が毎回変わるため、この名前を確実に使用することはできません。実際含まれるデータを{"590b14eb66b8f3786317e571": }
一部を削除し、のみを使用する方法はあります:
{
"updatedAt": "2017-05-04T11:47:55.000Z",
"created_at": "2017-05-04T11:47:55.000Z",
"UId": "590b10981da091b2618a4914",
"value": 37,
"_id": "590b14eb66b8f3786317e571",
"__v": 0
}
あるいはので、私はJSONオブジェクトからvalue
キーを取得することができますstring
を取得し解析する方法はありますか?
編集:イテレーションを使用して2つの異なるソリューションを試しましたが、同じエラーが表示されます。 (this質問に基づいて)最初のソリューション:
@Override
public void processFinish(JSONArray json) {
try {
if(json != null) {
JSONObject jo = new JSONObject(json.toString());
String temp;
for(int i = 0; i < json.length(); i++) {
JSONObject row = json.getJSONObject(i);
temp = row.getString("value");
temperature = Integer.parseInt(temp);
temperatureView.setText(temp + "°C");
checkTemperature();
}
}
}
}
これらのソリューションのどちらが問題を修正するので、私は問題は前の部分にあると仮定します。this質問に基づいて
@Override
public void processFinish(JSONArray json) {
try {
if(json != null) {
JSONObject jo = new JSONObject(json.toString());
Iterator<?> keys = jo.keys();
while(keys.hasNext()) {
String key = (String)keys.next();
if(jo.get(key) instanceof JSONObject) {
String temp = jo.getString("value");
temperature = Integer.parseInt(temp);
temperatureView.setText(temp + "°C");
checkTemperature();
}
}
}
}
}
第二の溶液、 processFinish
メソッドが呼び出されます。
私はこれらを使ってみましたが、 'value'キーを' JSONObject'から正常に取得できません。 – Alex
[JSONObjectを反復処理する方法]の複製がありますか?(http://stackoverflow.com/questions/9151619/how-to-iterate-over-a-jsonobject) –
huh? 'var value = jsonObject.get(key)'については – Selvin