0

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();
menu_on_top
  • 2,613
  • 14
  • 44
  • 71

3 Answers3

2

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

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • i should create the JSON Object in my for statement? – menu_on_top Apr 16 '13 at 13:04
  • in my example your string is "serverResponse". If you feed JSONObject a string it wil create an object from that string. That's the first step - the rest is just unpacking the rest of the data. – Rarw Apr 16 '13 at 13:08
  • the string i m getting comes from a JSON file in my server.I should put my link as "serverResponce"? – menu_on_top Apr 16 '13 at 13:11
  • Put the string there. You are downaloding that string from the server right? I don't see where you download it from but once you get that string from the server, the string goes into serverResponse. – Rarw Apr 16 '13 at 13:27
  • i have added my JSON parser – menu_on_top Apr 16 '13 at 13:32
  • I think what I'm looking for happens in your geJsonData method. That's where you downalod the string from the server before you parse it? – Rarw Apr 16 '13 at 13:40
1

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()); 
} 
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
-1

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());
Community
  • 1
  • 1
hd1
  • 33,938
  • 5
  • 80
  • 91