2017-10-14 10 views
-1

アンドロイドでオブジェクトのキーと値を取得する際の助けが必要です。アンドロイドでオブジェクトの戻り値を取得する方法

これは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" 
} 

アンドロイド使用して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

オブジェクトにフィールド用の独自のゲッターがありませんか? Productクラスを調べたり、製品を入力したりします。 、方法はありませんか? – 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); { //成功の場合は何かを実行する } else { //エラーが発生した場合は何かを実行してください() }。私はtrim()を追加し、それは働いた!ありがとうババ – Pa6

1

は、JavaクラスにあなたのJSONをマッピングし、JSONを使用して、それを解析するPOJOを作成します。以下は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; 
} 

} 

今のように使用してください。 JSONObject jsonObject = new JSONObject(productResponse); 1.use

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

製品productResponse = gson.toJson(product、Product.class);うん、私はpojoを持っています。今私は得る:シンボル製品を解決することができません。 – Pa6

+0

私はその愚かなことを知っていますが、pojoクラスの名前はProduct、rightですか? –

+0

はい、それを持っています – Pa6

0

String

はあなたのコードでこれを試してみてください判断するequalsIgnoreCase 2.use productResponse

を解析します。

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(); 
} 
関連する問題