gpt4 book ai didi

java - 将 JSONObject 获取到字符串数组值时出现问题

转载 作者:行者123 更新时间:2023-12-01 15:03:58 27 4
gpt4 key购买 nike

您好,我在获取 JSONObject 值标题并将其存储到数组中时遇到问题。

public class ViewPagerAdapter extends PagerAdapter 
{
// JSON Node names
private static final String TAG_CATEGORIESLIST = "categorylist";
private static final String TAG_TITLE = "title";
private static final String TAG_URL = "url";
public static String[] titles;
public final Context context;
public int[] scrollPosition;
JSONArray categories = null;
JSONObject json;
{
try {
JSONFunction JSONFunction = new JSONFunction();
json = JSONFunction.categorylist();
// Getting Array of Categories
categories = json.getJSONArray(TAG_CATEGORIESLIST);
// looping through All Categories
for(int i = 0; i < categories.length(); i++){
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
titles = new String[] {title}; //only obtain one result
scrollPosition = new int[titles.length];
}

} catch (JSONException e) {
e.printStackTrace();
}
}
...

我的titles只得到一个值,需要你们的帮助。
插件:

我正在使用Viewpagerindicator库,显示标题的默认代码是tat

private static String[] titles = new String[] { "Page 1", "Page 2", "Page 3", "Page 4", "Page 5"};

我正在尝试将 json 数据输入其中。谢谢。

最佳答案

您没有增加标题数组

改变这个

 titles = new String[] {title};//Because of this your last retrived  value will be stored in titles

 titles[i] = title;

关于java - 将 JSONObject 获取到字符串数组值时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13232452/

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