如何将 CustomerName 和 CustomerPhone 与我从 API 收到的字符串分开:
{
"CustomerPhone":"0300",
"CustomerName":"Saleh",
"CustomerPassword":"84CYmCulToJXo5KncGwSZa81acb2vbHjZ2IgUveMyeU=",
"Salt":"Q/IoQURM1Cv05wbkJjuo3w=="
}
最佳答案
以下是完成此操作的非常简单的步骤,
第 1 步:转到 http://www.jsonschema2pojo.org/并粘贴 JSON,现在选择选项“目标语言”为“Java”、“源类型 JSON”、“注释样式 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 对象,其中 response 变量将是您的 JSON 字符串。
CustomerData customerData = new Gson().fromJson(response,CustomerData.class);
customerData.getCustomerName();
customerData.getCustomerPhone();
关于java - 如何将 CustomerName 和 CustomerPhone 与我从 api 接收到的字符串分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55693206/