-1

私は JSONObject に苦労しています。私はすでにいくつかのjsonを返し、それをオブジェクトとオブジェクトのリストに正常に変換しました。私の今、私は立ち往生しています。

これは私が得ている JSONObject です:

{"Result":true,"Messages":["Goe bezig!"]}

メッセージを取得できますが、結果でブール値を取得できないようです。誰かがそれを取得する方法を説明できますか?

コードは次のとおりです。

public boolean Convert(JSONObject json) {
    try 
    {
        return json.getBoolean("Result");
    } 
    catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        return false;
    }
}
4

1 に答える 1

1

あなたの質問はかなり漠然としていますが、これは私にとってはうまくいきました。

    String jsonString = "{\"Result\":true,\"Messages\":[\"Goe bezig!\"]}";

    JSONObject jsonObject = new JSONObject(jsonString);

    boolean result = (Boolean) jsonObject.get("Result");

    System.out.println(result);

メソッドの最後でもキャッチしたい場合がありますException

try {
   return json.getBoolean("Result");
} catch (JSONException e) {
   e.printStackTrace(); // replace these with `Log` statement

   return false;
} catch (Exception e) {
   e.printStackTrace(); // replace these with `Log` statement

   return false;
}
于 2013-02-11T16:15:17.527 に答える