0

我有一个 Json 字符串。

每次打开我的应用程序并使用它们时,我都希望获得一个条目。在网上搜索,我创建了这样的东西:

ArrayList<HashMap<String, String>> Listads = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> mapads = new HashMap<String, String>();
        String randomValue = null;
        try {
            for (adsTrend tr : objs.getTrends())

            {

                Log.i("ADS",
                        tr.getId() + " - " + tr.getLink() + " - "
                                + tr.getType() + " - " + tr.getEnabled());

                lv_arr[i] = tr.getId() + " - " + tr.getLink() + " - "
                        + tr.getType() + " - " + tr.getEnabled();
                i++;

                mapads.put("id", tr.getId());
                mapads.put("link", tr.getLink());
                mapads.put("type", tr.getType());
                mapads.put("enabled", tr.getEnabled());

                Listads.add(mapads);
                Random generator = new Random();
                Object[] values = mapads.values().toArray();
                randomValue = (String) values[generator.nextInt(values.length)];

            }



            Toast.makeText(SplashActivity.this,"this is my random value : "+randomValue,Toast.LENGTH_LONG).show();
4

3 回答 3

2

假设您随机选择的所有内部JSON对象都包含相同的字符串(即启用和类型),选择随机对象很容易。您有一个嵌套JSONArray的内部JSONObject.

(1) 做出JSONObject初步反应

JSONObject response = new JSONObject(serverResponse);

(2) 提取趋势数组

JSONArray trends = response.getJSONArray("trends");

(3) 获取趋势数组的大小

int trendsSize = trends.length();

(4) 在 0 和数组大小 - 1 之间选择一个随机索引(因为包含 0)

Random r = new Random();
int randomObjectIndex = r.nextInt(trendsSize-0) + 0;

应该选择一个以trendSize为界的数字(该数字将不包括在内,因此它实际上是trendSize-1)和0

(5) 获取该位置的对象

JSONObject selectedRandomObject = trends.getJSONObject(randomObjectIndex);

(6) 提取你想要的字符串

String type = selectedRandomObject.getString("type");

只要您要查找的字符串在那里,您就不应该收到 JSONException

于 2013-04-16T13:01:56.673 回答
1

试试这样

JSONObject jsonObj = new JSONObject(jsonStr); 

// using JSONArray to grab the trendsfrom under popop 
 JSONArray menuitemArr = popupObject.getJSONArray("trends");  

// lets loop through the JSONArray and get all the items 
for (int i = 0; i < menuitemArr.length(); i++) { 
   // printing the values to the logcat 
      Log.v(menuitemArr.getJSONObject(i).getString("_id").toString()); 
      Log.v(menuitemArr.getJSONObject(i).getString("_link").toString()); 
      Log.v(menuitemArr.getJSONObject(i).getString("Enabled").toString()); 
} 
于 2013-04-16T13:00:29.857 回答
-1

将此与您的代码相结合:

ArrayList<HashMap<String, String>> Listads = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> mapads = new HashMap<String, String>();
        String randomValue = null;
        try {
            for (adsTrend tr : objs.getTrends())

            {

                Log.i("ADS",
                        tr.getId() + " - " + tr.getLink() + " - "
                                + tr.getType() + " - " + tr.getEnabled());

                lv_arr[i] = tr.getId() + " - " + tr.getLink() + " - "
                        + tr.getType() + " - " + tr.getEnabled();
                i++;

                mapads.put("id", tr.getId());
                mapads.put("link", tr.getLink());
                mapads.put("type", tr.getType());
                mapads.put("enabled", tr.getEnabled());

                Listads.add(mapads);
                Collections.shuffle(Listads);
                Listadds.get(0);

                Log.i("ADS",
                        tr.getId() + " - " + tr.getLink() + " - "
                                + tr.getType() + " - " + tr.getEnabled());
于 2013-04-16T12:58:42.520 回答