3 回答

TA貢獻1712條經驗 獲得超3個贊
String status ="";
JSONObject jsonObject = new JSONObject(response); //convert to json
if (jsonObject.has("status")){ //check if has the key
status = jsonObject.getString("status"); // get the value
}else{
}
Log.d("TAG", status); // do sth with the value
//Log => status

TA貢獻1780條經驗 獲得超1個贊
您從服務器接收 json 數據,因此您可以將其解析為 json,如前面的答案所示。更好的是,您可以使用Gson庫按如下方式解析數據,1-創建一個表示您的存儲庫的類,您可以使用 http://www.jsonschema2pojo.org/ 之類的工具來實現此目的,只需粘貼您的json字符串,然后從右側的選項中選擇Java作為目標語言,Json作為源類型,Gson作為注釋樣式, 并輸入要使用的任何類名,結果應類似于此包 com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Response {
@SerializedName("status")
@Expose
public String status;
@SerializedName("requestCount")
@Expose
public String requestCount;
@SerializedName("estelamCount")
@Expose
public String estelamCount;
}
然后,當您想要處理結果時,可以按以下步驟操作
Gson gson = new Gson();
//now you can parse the response string you received, here is responseString
Response response = gson.fromJson(responseString, Response.class);
//now you can access any field using the response object
Log.d("Reponse" , "status = " + response.status + ", requestCount = " + response.requestCount + ", estelamCount = " + response.estelamCount ;

TA貢獻1821條經驗 獲得超5個贊
我想你是在問解析你的回答,這就是你的做法
JSONObject myJson = new JSONObject(response);
String status = myJson.optString("status");
String count = myJson.optString("requestCount");
String estelamCount = myJson.optString("estelamCount");
添加回答
舉報