如何使用正则表达式通过服务器拆分响应


我想拆分通过服务器收到的响应,以便我可以取值,并在文本上设置。但我不能采取这些值...


响应 : {“status”:“no”,“requestCount”:“0”,“estelamCount”:“0”}


                    String[] split_model = response.split(",");

                  //  Log.i("split_model",split_model);

                    Log.i("phoneName", split_model[0]);

日志 == > I/电话名称: {“状态”:“否”


阿晨1998
浏览 176回答 3
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

慕神8447489

您从服务器接收 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")@Exposepublic String status;@SerializedName("requestCount")@Exposepublic String requestCount;@SerializedName("estelamCount")@Exposepublic String estelamCount;}然后,当您想要处理结果时,可以按以下步骤操作Gson gson = new Gson();//now you can parse the response string you received, here is responseStringResponse 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 ; 

收到一只叮咚

我想你是在问解析你的回答,这就是你的做法JSONObject myJson = new JSONObject(response);String status = myJson.optString("status");String count = myJson.optString("requestCount");String estelamCount = myJson.optString("estelamCount");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java