0

このJSONObjectをjavascriptで作成しています。

    var jsonBack = {id:userID,"dateToday":today, dateYesterday:yesterday,
                   wbsArrayToday:[{wbs:"13232323"}, {wbs:"13232324"}, {wbs:"13232325"}],
                   wbsArrayYesterday:[{wbs:"13232333"}, {wbs:"13232334"}, {wbs:"13232335"}]};

次に、Android アプリケーションでこれを呼び出します。

    JSONObject jsonObj = null;
    // Henter response data fra server vha. httpResponse
    HttpEntity entity1 = response.getEntity();
    if (entity1 != null) {
        InputStream is = null;
        try {
            is = entity1.getContent();
            // convert stream to string
            String result = Converter.convertStreamToString(is);

            //Remove []
            //if(result.startsWith("["))
            //  result = result.substring(1, result.length()-1);
            // Create JSON Object
            jsonObj = new JSONObject(result);
        } catch (IllegalStateException e) {
            e.printStackTrace();
            throw new HttpNodeClientException("HttpNodeClientException/IllegalStateException - createResponse():" + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            throw new HttpNodeClientException("HttpNodeClientException/IOException - createResponse():" + e.getMessage());
        } catch (JSONException e) {
            e.printStackTrace();
            throw new HttpNodeClientException("HttpNodeClientException/JSONException - createResponse():" + e.getMessage());
        } catch (ConverterException e){
            e.printStackTrace();
            throw new HttpNodeClientException("HttpNodeClientException/ConverterException - createResponse():" + e.getMessage());
        }

そして、JSONException が発生します。JSON を間違って設計しましたか?

例外は次のとおりです。

08-14 16:14:18.522: I/LoginActivity(418): HttpNodeClientException/JSONException - createResponse
():Value {"id":"11111111","dateToday":"14082012","dateYesterday":"13082012","wbsArrayToday":
[{"wbs":"13232323"},{"wbs":"13232324"},{"wbs":"13232325"}],"wbsArrayYesterday":
[{"wbs":"13232333"},{"wbs":"13232334"},{"wbs":"13232335"}]} of type java.lang.String cannot be 
converted to JSONObject
4

2 に答える 2

0

修正する必要がある唯一の行は次のとおりです。

jsonObj = new JSONObject(result);

これに:

jsonObj = new JSONObject(new JSONTokener(result));
于 2012-08-14T19:54:45.307 に答える
0

代わりにJSONTokenerクラスを使用してください。文字列を解析し、JSON オブジェクトを返します。

于 2012-08-14T16:28:32.043 に答える