menu_on_top
menu_on_top

Reputation: 2613

Get random item from Json String

I have a Json string .

i would like to get one entry every time i open my app and use them. Searching the net, i have created something like this:

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

Upvotes: 0

Views: 4349

Answers (3)

hd1
hd1

Reputation: 34667

Combining this with your code:

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

Upvotes: -1

Rarw
Rarw

Reputation: 7663

Assuming all of your internal JSON objects that you select randomly contain the same strings (i.e. enabled and type), selecting a random object is easy. You have a nested JSONArray that has some number of internal JSONObject.

(1) Make a JSONObject of the initial response

JSONObject response = new JSONObject(serverResponse);

(2) Extract the trends array

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

(3) Get the size of the trends array

int trendsSize = trends.length();

(4) Pick a random index between 0 and the array size - 1 (since 0 is included)

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

should pick a number bounded by the trendSize (that number will not be included so it is effectivly trendSize-1) and 0

(5) Get the object at that location

JSONObject selectedRandomObject = trends.getJSONObject(randomObjectIndex);

(6) Extract the strings you want

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

So long as the strings you are looking for are there you should not get a JSONException

Upvotes: 2

NagarjunaReddy
NagarjunaReddy

Reputation: 8645

try like this

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

Upvotes: 1

Related Questions