Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在没有数组字符串的情况下解析JSON数据?_Java_Android_Json_Parsing - Fatal编程技术网

Java 如何在没有数组字符串的情况下解析JSON数据?

Java 如何在没有数组字符串的情况下解析JSON数据?,java,android,json,parsing,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, "Titl

我试图从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);
        }
    }
}

如何循环通过jsonarray

编辑:

下面的内容应该在一个线程中

@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
}
@覆盖
受保护的void onPostExecute(字符串\u响应)
JSONArray jr=新JSONArray(“U响应”);

for(int i=0;i
JSONArray jsonPosts=mVideoData.getJSONArray(“”;
)是错误的。什么是
mVideoData
?“…我的JSON数据是一个没有字符串名称的数组。”:JSON数组没有“名称”-只有JSON对象有名称/值对。数组只是一个包含在
[…]中的“值”
。为了便于参考,本文在一个页面中解释了JSON语法/格式。之所以有.getJSONArray(“”),是因为JSON数据没有数组name@user3529614没有必要这样做。你可以简单地循环通过jsonarray。这是一个json数组,你可以问我如何循环通过jsonarray?好的,但是“json字符串”对于新的JSONArray不存在。json数据只是一个没有数组名称的方括号。@user3529614您的json在哪里。忘记名称。没有必要这样做。json字符串是您的JSONM我的json是“$id”:“1”,“ClubVideoId”:1027,“ClubId”:1,“Title”:“Brian访谈”,“ThumbURL”:“url”,“VideoURL”:“urll”,“DateAdd”:“2014-03-25 00:00”@user3529614不,你忘了
[{
。这些是键值对
“$id”:“1”,其中$id是键,值是1。我要将整个json数据添加到数组字符串中吗?包括[{
[  // 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;

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