0

i want to parse some json downloaded from the web in a JSONArray. Simple thing i think, but can't get it to work.

It seams that the JSON Format is the Problem. I tried different ways to fix it, but nothing helps.

I download the String with this method:

private String DownloadContent(String URL) {

    String result = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        HttpResponse response = httpClient.execute(httpGet);
        result = EntityUtils.toString(response.getEntity());
    } catch(Exception e) {
        Log.e("log_tag", e.toString());
    }
    return result;        
}

After that i try to parse the string to the JSONArray like this:

String resultString = DownloadContent(stringUrl);
JSONArray jArray = new JSONArray(resultString);

but everytime i get this exception:

exception

i test the following to get the JSON right:

result = EntityUtils.toString(response.getEntity(), "UTF-8");
result = StringEscapeUtils.unescapeJson(EntityUtils.toString(response.getEntity()));

this methods changed the string but no time the JSON is valid (tested with this).

In my JSON are things like this:

Meine gr\\u00f6\\u00dfte Leidenschaft ist es
http:\\/\\/bla.de\\/wp-content\\/uploads\\/2014\\/02\\/hello.jpg

What should i do to get it right? I hope someone can help me :) Thanks!

1
  • why do you need this StringEscapeUtils.unescapeJson? Commented Feb 4, 2014 at 20:00

1 Answer 1

2

Looking at the detailMessage, it looks like you are trying to create a JSONArray from JSON that looks like

{"posts": [...]}

That's a JSON object, not an array. You'll need to parse it as a JSONObject and get the array object from it.

JSONObject object = new JSONObject(resultString);
JSONArray array = object.getJSONArray("posts"); // or whatever the method is to get a JSONArray element from the JSONObject

This is assuming you are trying to get the JSON array named posts.

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

Comments

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.