3 回答

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
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貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
您從服務(wù)器接收 json 數(shù)據(jù),因此您可以將其解析為 json,如前面的答案所示。更好的是,您可以使用Gson庫(kù)按如下方式解析數(shù)據(jù),1-創(chuàng)建一個(gè)表示您的存儲(chǔ)庫(kù)的類(lèi),您可以使用 http://www.jsonschema2pojo.org/ 之類(lèi)的工具來(lái)實(shí)現(xiàn)此目的,只需粘貼您的json字符串,然后從右側(cè)的選項(xiàng)中選擇Java作為目標(biāo)語(yǔ)言,Json作為源類(lèi)型,Gson作為注釋樣式, 并輸入要使用的任何類(lèi)名,結(jié)果應(yīng)類(lèi)似于此包 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;
}
然后,當(dāng)您想要處理結(jié)果時(shí),可以按以下步驟操作
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貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
我想你是在問(wèn)解析你的回答,這就是你的做法
JSONObject myJson = new JSONObject(response);
String status = myJson.optString("status");
String count = myJson.optString("requestCount");
String estelamCount = myJson.optString("estelamCount");
添加回答
舉報(bào)