2017-10-14 96 views
-1

我想帮助获取android中的对象返回键和值。我如何获得对象的返回值在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(); 
        } 
+0

我不知道是否理解正确,但为什么不直接使用产品实例,例如。 product.getSuccess()?如果您想分析Json字符串并获取值,请查看json-simple:https://code.google.com/archive/p/json-simple/ – merterpam

+0

api返回该对象,在那个对象中结果键可以成功或失败,我想要得到结果值,以便我可以继续进行下一步相应的结果值。 – Pa6

+0

对象的字段是否有自己的getter?当您查看产品类或类型产品时。 ,有没有方法? – merterpam

回答

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 
} 
+0

JSONObject js = new JSONObject(productResponse); if(js.getString(“result”)。trim()。toString()。equals(“Success”)){ //做些什么以保证成功 } else { //在出错的情况下做些事情 }。我添加了修剪(),它的工作!谢谢巴巴 – Pa6

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(); 
        } 
+0

Product productResponse = gson.toJson(product,Product.class);是的,我有pojo。现在我得到了:不能解决符号产品。 – Pa6

+0

我知道它的愚蠢问,但pojo类有它的名字作为产品,对吗? –

+0

是的,它有它 – Pa6

0

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(); 
}