java - 如何将 CustomerName 和 CustomerPhone 与我从 api 接收到的字符串分开

标签 java android

如何将 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/

相关文章:

java - 将 Java 移植到 Ruby - 如何处理方法签名重载

java - 让 AutoCompleteTextView 响应 SimpleAdapter 中的更改

java - Android QR 扫描仪 : How to quit ZXingScannerView. ResultHandler 回到我来自的地方

java - 将类型级别验证错误消息附加到特定字段

java - 如何实现代理服务器?

java - TCP 客户端无法从服务器读取数据

java - Haxe:构建到 android 目标时崩溃

java - 我正在尝试获取图像的 ID 以在全 View 中显示它

android - 在 VS2017 中打开 Cordova 项目不会维护 Cordova 工具集版本

java - java中是否有并发&自过期&有序 HashMap