gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:08:56 27 4
gpt4 key购买 nike

我正在尝试从 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;
}'

最佳答案

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
}

正如注油建议

[  // 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
}

关于java - 没有数组字符串如何解析JSON数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23046589/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com