【问题标题】:How can I get values of object return in android如何在android中获取对象返回的值
【发布时间】:2017-10-14 11:07:05
【问题描述】:

我想获得有关在 android 中获取对象返回的键和值的帮助。

这是json对象,我想检查结果是否成功然后继续做其他事情。

{
  "product_name": "iPhone 8",
  "result": " Success",
  "error": null,
  "description": "iphone 8, the best phone",
  "agent": null,
  "client": "0700000000",
  "amount": "800$",
  "clientId": null,
  "stage": null,
  "message": " Success: YOUR AIRTIME HAS BEEN SENT TO:0727110300 Successfully.",
  "datetime": "2017-10-14 12:57:42"
}

这是我在 android 上使用 rx java 编写的代码

                @Override
                public void onNext(Product product) {

                    Gson gson = new Gson();
                    String productResponse = gson.toJson(product);
                    Toast.makeText(getContext(), productResponse, Toast.LENGTH_SHORT).show();
                    if (productResponse.getString("result").equals("success")) {
                        Toast.makeText(getContext(), "success, ...", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(getContext(), "error, ...", Toast.LENGTH_SHORT).show();
                    }

【问题讨论】:

  • 不知道理解对不对,为什么不直接使用product instance,例如。产品.getSuccess()?如果要解析 Json 字符串并获取值,请查看 json-simple:code.google.com/archive/p/json-simple
  • api 是返回那个对象,在那个对象中有一个结果键,可以是成功或失败,我想得到结果值,以便我可以根据结果值继续下一步。
  • 对象没有自己的字段获取器吗?当您查看产品类或类型产品时。 ,没有方法吗?

标签: android json api gson


【解决方案1】:

试试这个:

JSONObject js = new JSONObject(productResponse);
if(js.getString("result").toString().equals("Success")) {
     //Do something in case of success
}else{
      //Do something in case of error
}

【讨论】:

  • JSONObject js = new JSONObject(productResponse); if(js.getString("result").trim().toString().equals("Success")) { //成功时做点什么 }else{ //出错时做点 } .我添加了 trim() 并且成功了!谢谢爸爸
【解决方案2】:

创建一个 POJO 将您的 json 映射到 java 类并使用 JSon 解析它。以下是pojo。

public class Product {

@SerializedName("product_name")
@Expose
private String productName;
@SerializedName("result")
@Expose
private String result;
@SerializedName("error")
@Expose
private Object error;
@SerializedName("description")
@Expose
private String description;
@SerializedName("agent")
@Expose
private Object agent;
@SerializedName("client")
@Expose
private String client;
@SerializedName("amount")
@Expose
private String amount;
@SerializedName("clientId")
@Expose
private Object clientId;
@SerializedName("stage")
@Expose
private Object stage;
@SerializedName("message")
@Expose
private String message;
@SerializedName("datetime")
@Expose
private String datetime;

public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

public Object getError() {
return error;
}

public void setError(Object error) {
this.error = error;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Object getAgent() {
return agent;
}

public void setAgent(Object agent) {
this.agent = agent;
}

public String getClient() {
return client;
}

public void setClient(String client) {
this.client = client;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

public Object getClientId() {
return clientId;
}

public void setClientId(Object clientId) {
this.clientId = clientId;
}

public Object getStage() {
return stage;
}

public void setStage(Object stage) {
this.stage = stage;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getDatetime() {
return datetime;
}

public void setDatetime(String datetime) {
this.datetime = datetime;
}

}

现在像这样使用它。

 Gson gson = new Gson();
 Product productResponse = gson.toJson(product, Product.class);
if (productResponse.getResult().equals("success")) {
                        Toast.makeText(getContext(), "success, ...", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(getContext(), "error, ...", Toast.LENGTH_SHORT).show();
                    }

【讨论】:

  • 产品 productResponse = gson.toJson(product, Product.class);是的,我有pojo。现在我得到:无法解析符号产品。
  • 我知道问这个问题很愚蠢,但是 pojo 类的名称是 Product,对吗??
【解决方案3】:

1.使用JSONObject jsonObject = new JSONObject(productResponse);解析productResponse

2.使用equalsIgnoreCase判断String

在你的代码中试试这个。

Gson gson = new Gson();
String productResponse = gson.toJson(product);
Toast.makeText(getContext(), productResponse, Toast.LENGTH_SHORT).show();
try {
        JSONObject jsonObject = new JSONObject(productResponse);
        if (jsonObject.getString("result").equalsIgnoreCase("success")) {
            Toast.makeText(getContext(), "success, ...", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getContext(), "error, ...", Toast.LENGTH_SHORT).show();
        }
} catch (JSONException e) {
        e.printStackTrace();
}

【讨论】:

    猜你喜欢
    • 2011-10-21
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多