0

I have the following problem: I have a JSON File on a Server which I try to parse in Android. But i get the following error message:

06-13 19:24:39.025: E/JSON Parser(17169): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

Here is my JSON File:

    {
"settings":[
  {
     "rss":"true",
     "rss_feed":"http://test.com/rss.rss"
  }
],
 "map_locations":[
  {
     "title":"Büro Toronto",
     "address":"123 Younge Street Toronto"
  },
  {
     "title":"Büro New York",
     "address":"Time Square New York"
  }
]
}

And this is my Code:

        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(SETTINGS_URL);

        try {
            JSONObject c = json.getJSONArray("settings").getJSONObject(0);

            rss = c.getBoolean("rss");

            JSONArray jMap = json.getJSONArray("map_locations");
            for (int i = 0; i < jMap.length(); i++) {
                JSONObject c2 = jMap.getJSONObject(i);

                String map_title = c2.getString("title");
                String map_address = c2.getString("address");

                mapListTitle.add(map_title);
                mapListAddress.add(map_address);
            }

            URL_TO_RSSFEED = c.getString("rss_feed");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

Thanks for any help in advance!

The strange thing is that I didn't change anything (At my knowdledge) and it did work before. If you need any more information let me know!

Levon
  • 138,105
  • 33
  • 200
  • 191
user754730
  • 1,341
  • 5
  • 31
  • 62

2 Answers2

2

Wow ok I found the answer... Had to change

BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);

to

to BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);

Strange enought it did work a few hours before with the same settings.

But thanks alot for your help!

user754730
  • 1,341
  • 5
  • 31
  • 62
0

I ran this code and it runs fine.

String jsonStr = "{\"settings\":[{\"rss\":\"true\",\"rss_feed\":\"http://test.com/rss.rss\"}],\"map_locations\":[{\"title\":\"Büro Toronto\",\"address\":\"123 Younge Street Toronto\""
              +"},{\"title\":\"Büro New York\",\"address\":\"Time Square New York\"}]}";
    try {
    JSONObject json = new JSONObject(jsonStr);


        JSONObject c = json.getJSONArray("settings").getJSONObject(0);

        boolean rss = c.getBoolean("rss");

        JSONArray jMap = json.getJSONArray("map_locations");
        for (int i = 0; i < jMap.length(); i++) {
            JSONObject c2 = jMap.getJSONObject(i);

            String map_title = c2.getString("title");
            String map_address = c2.getString("address");

           /* mapListTitle.add(map_title);
            mapListAddress.add(map_address);*/
        }

       String URL_TO_RSSFEED = c.getString("rss_feed");
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();  
}

So I can only conclude that there might be some errors in the first jsonObject you are creating and I am talking about this line:

JSONObject json = jParser.getJSONFromUrl(SETTINGS_URL);

Arun George
  • 18,352
  • 4
  • 28
  • 28
  • 1
    I presume you might be using the `readJsonFromUrl(String url)` from this post http://stackoverflow.com/questions/4308554/simplest-way-to-read-json-from-a-url-in-java , the answer given by @RolandIllig. If yes, then you are getting in the error at this line `JSONObject json = new JSONObject(jsonText);`. – Arun George Jun 13 '12 at 18:39
  • Thanks that's what made me have a look inside my JSONParser class. You find my solution below. – user754730 Jun 13 '12 at 19:58