0

次のjson文字列をJSONObjectandroidに変換しようとしています。しかし、それはスローしJSONExceptionます。私は過去数時間、自分の心を掘り下げてきました。助けてください。

JSON 文字列:

parseExchangeRate({"query":
             {"count":1,"created":"2012-09-07T18:49:32Z","lang":"en-US","results":
                       {"row":{"rate":"55.395","name":"USD to INR"}}}});

例外:

Value <jsonobject>parseExchangeRate( of type java.lang.String cannot be converted to JSONObject

コード :

String result = convertStreamToString(instream);
Log.d(TAG, result); //this outputs the above stated string
JSONObject json = new JSONObject(result); // this line thows exception

前もって感謝します。

4

1 に答える 1

2

JSON文字列とは何ですか? あなたはそれが、

parseExchangeRate({"query":
              {"count":1,"created":"2012-09-07T18:49:32Z","lang":"en-US","results":
                        {"row":{"rate":"55.395","name":"USD to INR"}}}});

しかし、それはJavaScriptコードの行のように見えます. JSON != JavaScript. Web サービスがJSONP応答を返しているようです。それは問題ありませんが、あなたは JavaScript クライアントではないため、その応答から実際の JSON 文字列を解析する必要があります。

String result = convertStreamToString(instream);
Pattern p = Pattern.compile("^.*?\\((.*?)\\);$", Pattern.DOTALL);
Matcher m = p.matcher(result);
if (m.matches()) {
  String json = m.group(1);
  JSONObject jo = new JSONObject(json);
  ...
} else {
  // whoops
}
于 2012-09-07T19:05:58.533 に答える