【发布时间】: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 是返回那个对象,在那个对象中有一个结果键,可以是成功或失败,我想得到结果值,以便我可以根据结果值继续下一步。
-
对象没有自己的字段获取器吗?当您查看产品类或类型产品时。 ,没有方法吗?