0

I am trying to parse data from JSON but my JSON data is an array without a string name. Here is an example:

[
{
"$id": "1",
"ClubVideoId": 1027,
"ClubId": 1,
"Title": "Brian Interview",
"ThumbURL": "url",
"VideoURL": "urll",
"DateAdded": "2014-03-25 00:00"
},
{
"$id": "2",
"ClubVideoId": 1028,
"ClubId": 1,
"Title": "Chase Interview",
"ThumbURL": "url",
"VideoURL": "urll",
"DateAdded": "2014-03-25 00:00"
},

I can seem to pass an Array without a string. Here is my code:

public void handleBlogResponse() {
    mProgressBar.setVisibility(View.INVISIBLE);

    if (mVideoData == null) {
        updateDisplayForError();
    }
    else {
        try {
            JSONArray jsonPosts = mVideoData.getJSONArray("");
            ArrayList<HashMap<String, String>> clubVideos = 
                    new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < jsonPosts.length(); i++) {
                JSONObject post = jsonPosts.getJSONObject(i);
                String thumburl = post.getString(KEY_THUMBURL);
                thumburl = Html.fromHtml(thumburl).toString();
                String dateurl = post.getString(KEY_DATEADDED);
                dateurl = Html.fromHtml(dateurl).toString();

                HashMap<String, String> blogPost = new HashMap<String, String>();
                blogPost.put(KEY_THUMBURL, thumburl);
                blogPost.put(KEY_DATEADDED, dateurl);

                clubVideos.add(blogPost);
            }

            String[] keys = {KEY_THUMBURL, KEY_DATEADDED};
            int[] ids = { android.R.id.text1, android.R.id.text2 };
            SimpleAdapter adapter = new SimpleAdapter(this, clubVideos,
                    android.R.layout.simple_list_item_2, keys, ids);
            setListAdapter(adapter);

        } catch (JSONException e) {
            Log.e(TAG, "Exception caught!!", e);
        }
    }
}

Any Suggestions?

Here is where I'm pulling my json:

@Override
    protected JSONObject doInBackground(Object... arg0) {
    int responseCode = -1;  
    JSONObject jsonResponse = null;

    try {   
        URL blogFeedUrl = new URL("http://x.com/api/Club/Getx/1/?count=" + NUMBER_OF_POSTS);
        HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
        connection.connect();

        responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = connection.getInputStream();
            Reader reader = new InputStreamReader(inputStream);
            int contentLength = connection.getContentLength();
            char[] charArray = new char[contentLength];
            reader.read(charArray);
            String responseData = new String(charArray);

            jsonResponse = new JSONObject(responseData);
        }
        else {
            Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
        }
        Log.i(TAG, "Code: " + responseCode);
    }
    catch (MalformedURLException e) {
        logException(e);
    }
    catch (IOException e) {
        logException(e);
    }
    catch (Exception e) {
        logException(e);
    }

    return jsonResponse;
}'
6
  • 1
    JSONArray jsonPosts = mVideoData.getJSONArray(""); is wrong. what is mVideoData? Commented Apr 13, 2014 at 18:27
  • "...my JSON data is an array without a string name." : JSON arrays don't have 'names' - it's only JSON objects which have name/value pairs. An array is simply a 'value' enclosed in [...]. For reference this pretty much explains JSON syntax / formatting in one page json.org Commented Apr 13, 2014 at 18:31
  • the .getJSONArray("") is there because the JSON data doesn't have an array name Commented Apr 13, 2014 at 18:38
  • @user3529614 there is no need for that. You can simply loop through the jsonarray. its a json array that you ahve Commented Apr 13, 2014 at 18:40
  • how do I loop through a jsonarray? Commented Apr 13, 2014 at 18:40

1 Answer 1

1

how do I loop through a jsonarray?

JSONArray jr = new JSONArray("json string");
for(int i=0;i<jr.length();i++)
{
   JSONObject jb = (JSONObject) jr.get(i);
   String id =jb.getString("$id");
   String clubvid = jb.getString("ClubVideoId");
   ...// similarly others
}

As squonk suggested

[  // json array node
{   // json object node 
"$id": "1",  

Edit:

The below should be in a thread

@Override 
protected String doInBackground(Object... arg0)
{
String _response=null;
try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("http://sql.gamedayxtra.com/api/Club/GetClubVideos/1");
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
_response=EntityUtils.toString(resEntity);
}catch(Exception e)
{
    e.printStackTrace();
}
return _response;

}

In onPostExecute

@Override 
protected void onPostExecute(String _response)
JSONArray jr = new JSONArray("_response");
for(int i=0;i<jr.length();i++)
{
   JSONObject jb = (JSONObject) jr.get(i);
   String id =jb.getString("$id");
   String clubvid = jb.getString("ClubVideoId");
   ...// similarly others
}
Sign up to request clarification or add additional context in comments.

10 Comments

ok but the "json string" for the new JSONArray doesnt exist. The json data is just a square bracket with no array name.
@user3529614 where is your json. forget the name. there is no need for that. json string is your json
my json is "$id": "1", "ClubVideoId": 1027, "ClubId": 1, "Title": "Brian Interview", "ThumbURL": "url", "VideoURL": "urll", "DateAdded": "2014-03-25 00:00"
@user3529614 nope you forgot [{. these are key value pairs "$id": "1", where $id is the key and the value is 1
Do i add the entire json data into the array string? Including the [{
|

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.