0

Im creating this JSONObject in javascript.

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

Then i call this in my android application.

    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());
        }

And i get an JSONException. Did i design the JSON wrong?

Heres the exception:

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
user1581164
  • 71
  • 2
  • 5
  • The JSON also gets validated at http://jsonlint.com/!! – user1581164 Aug 14 '12 at 16:24
  • In short, you need way more double quotes. – robbieAreBest Aug 14 '12 at 16:30
  • The JSON puked up in the `Exception` is fine. Do a `println` of the JSON String before it goes to `JSONObject`. – pb2q Aug 14 '12 at 16:31
  • Take a look at my answer in [HTTP Get Request](http://stackoverflow.com/questions/11526437/android-illegalstateexception-in-httpget/11526693#11526693). It may help you to parse the json string from a Web Service. – Ali Aug 14 '12 at 16:31
  • Ali thanks.. Im already doing it the same way. It just wont accept the the string when parsing to JSON – user1581164 Aug 14 '12 at 19:05

2 Answers2

0

Use the JSONTokener class instead. It parses a string and return a JSON object.

HonkyTonk
  • 1,961
  • 11
  • 11
  • So you use JSONTokener, if the JSON is complex(Containing arrays)? – user1581164 Aug 14 '12 at 20:02
  • @user1581164 Not too sure about the kissing bit but glad I could help. :) The JSONTokener gives you more control over the parsing since it can iterate over all tokens in the JSON object _or_ the actual tokens that are in the string to be parsed. That is all. – HonkyTonk Aug 15 '12 at 08:00
0

the only line you need to fix is this one:

jsonObj = new JSONObject(result);

into this one:

jsonObj = new JSONObject(new JSONTokener(result));
Ariel Capozzoli
  • 775
  • 1
  • 6
  • 15
  • JSONArray locations = object.getJSONArray("wbsArrayToday"); This will give me a JSONArray. But as you see, my JSONArray have an id(which is called "wbs") and a value. How to get the value by pointing at the id and index? Cause locations.getString(1) gives both values... – user1581164 Aug 15 '12 at 06:58
  • in this case i would use for(int i = 0; i – Ariel Capozzoli Aug 15 '12 at 14:24