Dandrews
Dandrews

Reputation: 618

Android Parse JSON string to String array

I am trying to parse a json string passed back from a web server and turn it into a keyed string array. The result hopefully looking like "str[ID] = artist - title".

Here is the code I'm using to get the json and start to parse it.

                new Thread(new Runnable() {
                public void run() {
                    try {
                        try {
                            HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
                            HttpGet httpget = new HttpGet("http://somesite.net/some.php"); // Set the action you want to do
                            HttpResponse response = httpclient.execute(httpget); // Executeit
                            HttpEntity entity = response.getEntity();
                            InputStream is = entity.getContent(); // Create an InputStream with the response
                            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                            StringBuilder sb = new StringBuilder();
                            String line = null;
                            while ((line = reader.readLine()) != null) // Read line by line
                                sb.append(line + "\n");


                            String resString = sb.toString(); // Result is here
                            JSONObject json = new JSONObject(resString);
                            String[] array = new Gson().fromJson(resString, String[].class);
                            Log.e("TESTAPP", "SOME RES E "  + array.toString());

                            is.close(); // Close the stream
                        }
                        catch (Exception e) {
                            Log.e("TESTAPP", "SOME Catch Error E "  + e.toString());
                        }
                    }
                    catch (Exception e){
                        Log.e("TESTAPP", "SOME Error" + e.getMessage());
                    }
                }
            }).start();

Some sample json being used

{"item":{"artist":"Billy Idol","requestid":"42207","title":"Rebel Yell"}}{"item":{"artist":"Black Sunshine","requestid":"42208","title":"Once In My Life"}}{"item":{"artist":"Blackstreet","requestid":"42209","title":"Before I Let You Go"}}{"item":{"artist":"Black Sabbath","requestid":"42210","title":"Time Machine"}}

Upvotes: 0

Views: 186

Answers (1)

Shivam Verma
Shivam Verma

Reputation: 8023

There's an error in the JSON String that you're trying to parse. Every String inside {} is a JSON Object and in the provided String there's an object which contains another item object :

{
  "item": {
    "artist": "Billy Idol",
    "requestid": "42207",
    "title": "Rebel Yell"
  }
}

And then just another object which contains another item object.

{
  "item": {
    "artist": "Black Sunshine",
    "requestid": "42208",
    "title": "Once In My Life"
  }
}

This is an invalid JSONString. I'd suggest you to use tools like : http://jsonlint.com/ to validate your JSON String before trying to parse.

Upvotes: 1

Related Questions