4 回答

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
以下是完成它的非常簡(jiǎn)單的步驟,
第 1 步:轉(zhuǎn)到http://www.jsonschema2pojo.org/并粘貼您的 JSON,現(xiàn)在選擇選項(xiàng) Target language as Java、Source Type JSON 、Annotation Style GSON。然后按預(yù)覽按鈕,將模型復(fù)制到剪貼板。
第 2 步:現(xiàn)在在您的項(xiàng)目中添加 GSON 庫(kù)
第 3 步:使用名稱 CustomerData 或您想要的任何名稱創(chuàng)建模型類,然后從剪貼板粘貼代碼。
它看起來(lái)很像
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 步:現(xiàn)在您必須通過(guò)以下代碼將 JSON 解析為 GSON 對(duì)象,其中響應(yīng)變量將是您的 JSON 字符串。
CustomerData customerData = new Gson().fromJson(response,CustomerData.class);
customerData.getCustomerName();
customerData.getCustomerPhone();

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
嘗試這個(gè):
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();
}

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
對(duì)于您給定的字符串,下面的代碼可以正常工作,我已經(jīng)測(cè)試過(guò)了,它非常自我解釋。
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)

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
你有幾個(gè)選擇。
使用像 GSON 這樣的簡(jiǎn)單 JSON 庫(kù)并創(chuàng)建一個(gè)模型,您只需將字符串轉(zhuǎn)換為該模型即可
使用 Android JsonElements 并從字符串創(chuàng)建一個(gè) JsonElement 并按名稱遍歷每個(gè)孩子,直到獲得所需的孩子。
最丑陋的方式,但你可以做到,通過(guò)已知字符串解析字符串。(未經(jīng)測(cè)試,你必須調(diào)整這個(gè),但我真的希望你不要走這條路哈哈)
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)
添加回答
舉報(bào)