私はAndroidの初心者です。私のプロジェクトでは、HTTPレスポンスから以下のjsonを取得しています。
[{"Date":"2012-1-4T00:00:00",
"keywords":null,
"NeededString":"this is the sample string I am needed for my project",
"others":"not needed"}]
上記のjsonから"NeededString"を取得したいです。どうすれば取得できますか?
これが役に立つかもしれない。
Java:
JSONArray arr = new JSONArray(result);
JSONObject jObj = arr.getJSONObject(0);
String date = jObj.getString("NeededString");
Kotlin:
val jsonArray = JSONArray(result)
val jsonObject: JSONObject = jsonArray.getJSONObject(0)
val date= jsonObject.get("NeededString")
JSONArrayを取得し、ループを使って Array 内の
JSONObject` を繰り返し処理するだけです。
JSONArray mArray;
try {
mArray = new JSONArray(responseString);
for (int i = 0; i < mArray.length(); i++) {
JSONObject mJsonObject = mArray.getJSONObject(i);
Log.d("OutPut", mJsonObject.getString("NeededString"));
}
} catch (JSONException e) {
e.printStackTrace();
}
getString
を使用できます。
String name = jsonObject.getString("name");
// it will throws exception if the key you specify doesn't exist
または「optString」。
String name = jsonObject.optString("name");
// it will returns the empty string ("") if the key you specify doesn't exist
プロジェクトに org.json.jsonobject
をインクルードする。
そうすれば、こうなる:
JSONObject jresponse = new JSONObject(responseString);
responseString = jresponse.getString("NeededString");
仮に、responseString
が受信したレスポンスを保持しているとします。
受信したレスポンスをStringに変換する方法を知りたい場合は、こちらを参照してください:
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
ResponseEntity
から要素を取得するためのコードです。
try {
final ResponseEntity<String> responseEntity = restTemplate.exchange(API_URL, HttpMethod.POST, entity, String.class);
log.info("responseEntity"+responseEntity);
final JSONObject jsonObject ;
if (responseEntity.getStatusCode() == HttpStatus.CREATED) {
try {
jsonObject = new JSONObject(responseEntity.getBody());
final String strName = jsonObject.getString("name");
log.info("name:"+strName);
} catch (JSONException e) {
throw new RuntimeException("JSONException occurred");
}
}
}catch (HttpStatusCodeException exception) {
int statusCode = exception.getStatusCode().value();
log.info("statusCode:"+statusCode);
}
JSONObjectライブラリを使用できる場合は、そのままにしておくことができます。
JSONArray ja = new JSONArray("[{\"Date\":\"2012-1-4T00:00:00\",\"keywords\":null,\"NeededString\":\"this is the sample string I am needed for my project\",\"others\":\"not needed\"}]");
String result = ja.getJSONObject(0).getString("NeededString");
上記の回答に触発されましたが、もう少し詳しく、以下の私の回答をご覧ください。..
// Get The Json Response (With Try Catch)
try {
String s = null;
if (response.body() != null) {
s = response.body().string();
// Convert Response Into Json Object (With Try Catch)
JSONObject json = null;
try {
json = new JSONObject(s);
// Extract The User Id From Json Object (With Try Catch)
String stringToExtract = null;
try {
stringToExtract = json.getString("NeededString");
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
それはあなたにとって役に立ったと思います。
JSONArray jre = objJson.getJSONArray("Result");
for (int j = 0; j < jre.length(); j++) {
JSONObject jobject = jre.getJSONObject(j);
String date = jobject.getString("Date");
String keywords=jobject.getString("keywords");
String needed=jobject.getString("NeededString");
}
これが私が使用したソリューションです。 文字列からJSONを取得するための作品です。
protected String getJSONFromString(String stringJSONArray) throws JSONException {
return new StringBuffer(
new JSONArray(stringJSONArray).getJSONObject(0).getString("cartype"))
.append(" ")
.append(
new JSONArray(employeeID).getJSONObject(0).getString("model"))
.toString();
}