如何将 CustomerName 和 CustomerPhone 从我从 api 接收的字符串中分

如何将 CustomerName 和 CustomerPhone 从我从 API 接收的字符串中分离出来:


{

  "CustomerPhone":"0300",

  "CustomerName":"Saleh",

  "CustomerPassword":"84CYmCulToJXo5KncGwSZa81acb2vbHjZ2IgUveMyeU=",

  "Salt":"Q/IoQURM1Cv05wbkJjuo3w=="

}


千万里不及你
浏览 207回答 4
4回答

开满天机

以下是完成它的非常简单的步骤,第 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")@Exposeprivate String customerPhone;@SerializedName("CustomerName")@Exposeprivate String customerName;@SerializedName("CustomerPassword")@Exposeprivate String customerPassword;@SerializedName("Salt")@Exposeprivate 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();

吃鸡游戏

尝试这个: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();}

慕田峪7331174

对于您给定的字符串,下面的代码可以正常工作,我已经测试过了,它非常自我解释。    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)

MYYA

你有几个选择。使用像 GSON 这样的简单 JSON 库并创建一个模型,您只需将字符串转换为该模型即可使用 Android JsonElements 并从字符串创建一个 JsonElement 并按名称遍历每个孩子,直到获得所需的孩子。最丑陋的方式,但你可以做到,通过已知字符串解析字符串。(未经测试,你必须调整这个,但我真的希望你不要走这条路哈哈)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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java