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

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

如何將 CustomerName 和 CustomerPhone 從我從 api 接收的字符串中分

如何將 CustomerName 和 CustomerPhone 從我從 api 接收的字符串中分

千萬里不及你 2022-10-07 16:17:52
如何將 CustomerName 和 CustomerPhone 從我從 API 接收的字符串中分離出來:{  "CustomerPhone":"0300",  "CustomerName":"Saleh",  "CustomerPassword":"84CYmCulToJXo5KncGwSZa81acb2vbHjZ2IgUveMyeU=",  "Salt":"Q/IoQURM1Cv05wbkJjuo3w=="}
查看完整描述

4 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

以下是完成它的非常簡單的步驟,

第 1 步:轉到http://www.jsonschema2pojo.org/并粘貼您的 JSON,現在選擇選項 Target language as Java、Source Type JSON 、Annotation Style GSON。然后按預覽按鈕,將模型復制到剪貼板。

第 2 步:現在在您的項目中添加 GSON 庫

第 3 步:使用名稱 CustomerData 或您想要的任何名稱創建模型類,然后從剪貼板粘貼代碼。

它看起來很像

public class CustomerData {


@SerializedName("CustomerPhone")

@Expose

private String customerPhone;

@SerializedName("CustomerName")

@Expose

private String customerName;

@SerializedName("CustomerPassword")

@Expose

private String customerPassword;

@SerializedName("Salt")

@Expose

private String salt;


public String getCustomerPhone() {

return customerPhone;

}


public void setCustomerPhone(String customerPhone) {

this.customerPhone = customerPhone;

}


public String getCustomerName() {

return customerName;

}


public void setCustomerName(String customerName) {

this.customerName = customerName;

}


public String getCustomerPassword() {

return customerPassword;

}


public void setCustomerPassword(String customerPassword) {

this.customerPassword = customerPassword;

}


public String getSalt() {

return salt;

}


public void setSalt(String salt) {

this.salt = salt;

}


}

第 4 步:現在您必須通過以下代碼將 JSON 解析為 GSON 對象,其中響應變量將是您的 JSON 字符串。


CustomerData customerData = new Gson().fromJson(response,CustomerData.class);

customerData.getCustomerName();

customerData.getCustomerPhone();


查看完整回答
反對 回復 2022-10-07
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

嘗試這個:


String jsonText = "{\"CustomerPhone\":\"0300\",\"CustomerName\":\"Saleh\",\"CustomerPassword\":\"84CYmCulToJXo5KncGwSZa81acb2vbHjZ2IgUveMyeU=\",\"Salt\":\"Q/IoQURM1Cv05wbkJjuo3w==\"}";

try {

    JSONObject jsonObj = new JSONObject(jsonText);

    String CustomerPhone = jsonObj.getString("CustomerPhone");

    String CustomerName = jsonObj.getString("CustomerName");

} catch (JSONException e){

    e.printStackTrace();

}


查看完整回答
反對 回復 2022-10-07
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

對于您給定的字符串,下面的代碼可以正常工作,我已經測試過了,它非常自我解釋。


    val jsonObject = JSONObject(jsonString)

    val phone = jsonObject.getString("CustomerPhone")

    val name = jsonObject.getString("CustomerName")

    val password = jsonObject.getString("CustomerPassword")

    val salt = jsonObject.getString("Salt")


    Log.d("phone", phone)

    Log.d("name", name)

    Log.d("password", password)

    Log.d("salt", salt)


查看完整回答
反對 回復 2022-10-07
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

你有幾個選擇。

  1. 使用像 GSON 這樣的簡單 JSON 庫并創建一個模型,您只需將字符串轉換為該模型即可

  2. 使用 Android JsonElements 并從字符串創建一個 JsonElement 并按名稱遍歷每個孩子,直到獲得所需的孩子。

  3. 最丑陋的方式,但你可以做到,通過已知字符串解析字符串。(未經測試,你必須調整這個,但我真的希望你不要走這條路哈哈)

var customerStartPhoneIndex = jsonString.indexOf("CustomerPhone\":\")

var customerStartNameIndex = jsonString.indexOf("CustomerName\":\")

var customerEndphoneIndex = jsonString.indexOf(",")

var customerEndNameIndex = jsonString.indexOf(",", str.indexOf(",") + 1)

var customerPhone = jsonString.subString(customerStartPhoneIndex, customerEndPhoneIndex)

var customerName = jsonString.substring(customerStartNameIndex, customerEndNameIndex)


查看完整回答
反對 回復 2022-10-07
  • 4 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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