如何解析没有数组字符串的JSON数据?

时间:2014-04-13 18:25:09

标签: java android json parsing

我正在尝试解析JSON中的数据,但我的JSON数据是一个没有字符串名称的数组。这是一个例子:

[
{
"$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"
},

我似乎可以在没有字符串的情况下传递数组。这是我的代码:

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

任何建议?

这是我拉我的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;
}'

1 个答案:

答案 0 :(得分:1)

  

如何循环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
}

正如squonk建议的那样

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

编辑:

下面应该是一个帖子

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

}

在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
}