亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用正則表達式通過服務器拆分響應

如何使用正則表達式通過服務器拆分響應

阿晨1998 2022-09-14 17:58:20
我想拆分通過服務器收到的響應,以便我可以取值,并在文本上設置。但我不能采取這些值...響應 : {“status”:“no”,“requestCount”:“0”,“estelamCount”:“0”}                    String[] split_model = response.split(",");                  //  Log.i("split_model",split_model);                    Log.i("phoneName", split_model[0]);日志 == > I/電話名稱: {“狀態”:“否”
查看完整描述

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


查看完整回答
反對 回復 2022-09-14
?
慕神8447489

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 ; 


查看完整回答
反對 回復 2022-09-14
?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

我想你是在問解析你的回答,這就是你的做法


JSONObject myJson = new JSONObject(response);


String status = myJson.optString("status");

String count = myJson.optString("requestCount");

String estelamCount = myJson.optString("estelamCount");


查看完整回答
反對 回復 2022-09-14
  • 3 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號