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");
添加回答
举报
