我是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”。如何获得它?
答案 0 :(得分:59)
这可能会对你有帮助。
<强> 爪哇: 强>
JSONArray arr = new JSONArray(result);
JSONObject jObj = arr.getJSONObject(0);
String date = jObj.getString("NeededString");
<强> 科特林: 强>
val jsonArray = JSONArray(result)
val jsonObject: JSONObject = jsonArray.getJSONObject(0)
val date= jsonObject.get("NeededString")
答案 1 :(得分:9)
你只需要获取JSONArray
并使用循环迭代数组内的JSONObject
,但在你的情况下它只有一个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();
}
答案 2 :(得分:4)
在项目中加入org.json.jsonobject
。
然后你可以这样做:
JSONObject jresponse = new JSONObject(responseString);
responseString = jresponse.getString("NeededString");
假设responseString
保留您收到的回复。
如果您需要知道如何将收到的响应转换为字符串,请按以下步骤操作:
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
答案 3 :(得分:3)
您可以使用public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
int[] nums = new int[3];
System.out.print ("Enter a number: ");
String num = Scan.nextLine();
num = num.trim();
for(int i = 0; i < num.length(); i++){
int digit = Integer.parseInt("" + num.charAt(i));
if(digit == 0){
nums[0]++;
}else if(digit % 2 == 0){
nums[2]++;
}else{
nums[1]++;
}
}
System.out.print("The number: " + num + " has " + nums[0] + " zeros, "
+ nums[2] + " evens, " + "and " + nums[1] + " odds.");
Scan.close();
}
getString
或String name = jsonObject.getString("name");
// it will throws exception if the key you specify doesn't exist
optString
答案 4 :(得分:1)
如果您可以使用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");
答案 5 :(得分:0)
我认为这对你有帮助
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");
}
答案 6 :(得分:0)
这是我用过的解决方案 适用于从字符串
获取JSONprotected 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();
}
答案 7 :(得分:0)
这是从ResponseEntity获取元素的代码
try {
final ResponseEntity 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);
}
答案 8 :(得分:0)
请参阅以下答案,以上面的答案为灵感,但要更详细...
// 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();
}