1

i am work on an online credit topup application. now i want to fetch the denomination available to a particular type of voucher which is select by.this is the scenario a user selects a type of voucher(tigo,mtn,vodafon,airtel etc) from a combo box.after the selection the voucher denomination combobox should be populated with all denominations available. for now am out putting with json .av tried with this code but it returns this:

 @RequestMapping(value = "/rest/getdenominations")
        public @ResponseBody
        String getDenominationByType(ModelMap model) throws JSONException {
            Long vouchertypeId = 1L;
            JSONArray typeDenominationsArray = new JSONArray();
            for (Voucher voucher : voucherController.getTypDenominationAvailable(vouchertypeId)) {
                JSONObject voucherJSON = new JSONObject();
                voucherJSON.put(" ", voucher.getDenomination());
                typeDenominationsArray.put(voucherJSON);
            }

            return typeDenominationsArray.toString();
        }

but it returns this

[{" ":10},{" ":2},{" ":1},{" ":10},{" ":2}]

what i want is that the 2 and 10 should be displayed just once. need help to this..tanx in advance

Samuel Anertey
  • 171
  • 1
  • 2
  • 7
  • What does `voucherController.getTypDenominationAvailable(vouchertypeId)` do? Appears to return those 5 `vouchers`. – Java Devil Jul 16 '13 at 23:07

1 Answers1

0

I don't think that the JSONArray object has a contains(JSONObject obj) function but you can make one yourself (See this link for an example). With this, inside of the for loop after you create each JSONObject check if your JSONArray contains the object. If not, add it, otherwise dont add it. This is not the most elegant solution but it should work.

Community
  • 1
  • 1
Scott
  • 1,652
  • 1
  • 13
  • 10
  • aryt will try this nd let u knw de outcome – Samuel Anertey Jul 16 '13 at 23:39
  • hi scout...i created this method and call it my getDenominationByType() method....public boolean denominationExists(JSONArray theJSONArray, BigDecimal denomination) { return theJSONArray.toString().contains("\" \":\""+denomination+"\""); } – Samuel Anertey Jul 17 '13 at 07:36
  • I looks about right, but in this function you have surrounded `denomination` with quotes. In your json above there are not quotes around the `denomination`. Does this work? – Scott Jul 17 '13 at 16:50
  • scout please cud u please clarify me on this...am new to json – Samuel Anertey Jul 17 '13 at 21:39
  • Try this: getDenominationByType() method....public boolean denominationExists(JSONArray theJSONArray, BigDecimal denomination) { return theJSONArray.toString().contains("\" \":"+denomination); } – Scott Jul 17 '13 at 22:05
  • Please make an edit with in your question with your entire code – Scott Jul 18 '13 at 23:53
  • yeah scout tanx for de help...but i av resolved dis....i jst changed my query in de repository class....i used select distinct kedv.denomination..............nd it worked – Samuel Anertey Jul 19 '13 at 00:26
  • http://stackoverflow.com/questions/17735660/how-to-set-limit-for-a-hibernate-query/17735918?noredirect=1#17735918 – Samuel Anertey Jul 19 '13 at 00:29
  • http://stackoverflow.com/questions/17735660/how-to-set-limit-for-a-hibernate-query/17735918?noredirect=1#17735918 – Samuel Anertey Jul 19 '13 at 00:30
  • plss make a suggestion – Samuel Anertey Jul 19 '13 at 00:31