13

次のコードを実行すると...

  JSONObject jsonObject = null;
  JSONParser parser=new JSONParser(); // this needs the "json-simple" library

  try 
  {
        Object obj = parser.parse(responseBody);
        jsonObject=(JSONObject)obj;
  }
  catch(Exception ex)
  {
        Log.v("TEST","Exception1: " + ex.getMessage());
  }

...実行時に、ログ出力に次のように表示されます。

Exception1: org.json.simple.JSONObject cannot be cast to org.json.JSONObject

理由がわかりません。

4

3 に答える 3

30

間違ったクラスをインポートしました。変化する

import org.json.JSONObject;

import org.json.simple.JSONObject;
于 2013-02-08T12:40:55.957 に答える
4

コードを次のように変更します。

  org.json.simple.JSONObject jsonObject = null;
  JSONParser parser=new JSONParser(); // this needs the "json-simple" library

  try 
  {
        Object obj = parser.parse(responseBody);
        jsonObject=(org.json.simple.JSONObject)obj;
  }
  catch(Exception ex)
  {
        Log.v("TEST","Exception1: " + ex.getMessage());
  }

org.json.simpleまたは、 json文字列の解析にライブラリのみを使用している場合は、org.json.simple.* 代わりにインポートするだけです。org.json.JSONObject

于 2013-02-08T12:43:01.580 に答える
0

を使用したい場合は、org.json.JSONObjectこれを置き換えることでそれを行うことができます:

JSONObject jsonObject = null;
JSONParser parser=new JSONParser(); // this needs the "json-simple" library

try 
{
    Object obj = parser.parse(responseBody);
    jsonObject=(JSONObject)obj;
}
catch(Exception ex)
{
    Log.v("TEST","Exception1: " + ex.getMessage());
}

と:

JSONObject jsonObject = null;
JSONParser parser=new JSONParser(); // this needs the "json-simple" library

try 
{
    String obj = parser.parse(responseBody).toString(); //converting Object to String
    jsonObject = new JSONObject(obj); // org.json.JSONObject constructor accepts String as an argument
}
catch(Exception ex)
{
    Log.v("TEST","Exception1: " + ex.getMessage());
}
于 2022-01-20T08:18:13.480 に答える