如何在android中获取对象返回值

时间:2017-10-14 11:07:05

标签: android json api gson

我想帮助获取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();
                    }

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:1)

创建一个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();
                    }

答案 2 :(得分:1)

  

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();
}
相关问题