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!

3
  • What exactly do you mean? Like UTF-8 problems or something? I also tried it with "ue" instead of "ü" but that didn't change anything... Commented Jun 13, 2012 at 18:00
  • @user754730 : you can check json return by api is valid or not from here jsonviewer.stack.hu Commented Jun 13, 2012 at 18:17
  • The JSON actually looks fine on the website there... Commented Jun 13, 2012 at 19:47

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

I presume you might be using the readJsonFromUrl(String url) from this post stackoverflow.com/questions/4308554/… , the answer given by @RolandIllig. If yes, then you are getting in the error at this line JSONObject json = new JSONObject(jsonText);.
Thanks that's what made me have a look inside my JSONParser class. You find my solution below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.