0

am trying to read List of HashMap which is sent from java to javascript, in javascript its taking as string, am not able to break that further because that will make logic too complicated.

List<HashMap<String,String>> tailDetails = new ArrayList<HashMap<String,String>>();  
    while(tailoredDetails.next())
     {
        HashMap<String,String> each = new HashMap<String,String>();
        each.put(tailoredDetails.getString("RECORD"),
        tailoredDetails.getString("BILLABLE"));
        tailDetails.add(each);
     }
TailoredObjRepVO.setTailoredRecords(tailDetails);
arlTailoredTransRecords.add(TailoredObjRepVO);

This is the data that i want to read in Javascript and display as table

am reading like this in javascript

var message = document.all.item("tailoredAdjustmentItemsList["+length+"]["+breadth+"].tailoredRecords").value;

When I check the source from application it is like this

<input type="hidden" name="tailoredAdjustmentItemsList[0][0].tailoredRecords" value="[{TUT7R                     BOMKMG1   =Y}, {TUT7R                     BOMWUH1   =Y}, {TUT7R                     DACKMG1   =Y}, {TUT7R                     DACNGB1   =Y}, {TUT7R                     DELKMG1   =Y}, {TUT7R                     KMGBOM1   =Y}, {TUT7R                     KMGDAC1   =Y}, {TUT7R                     KMGDEL1   =Y}, {TUT7R                     KMGMAA1   =Y}, {TUT7R                     MAAKMG1   =Y}, {TUT7R                     MAAWUH1   =Y}, {TUT7R                     WUHBOM1   =Y}, {TUT7R                     WUHMAA1   =Y}]">

I tried JSONObject, but the problem here is JSON Jar is not there in our project if I now go ahead and add we have to test the complete application for this change which we are not ready to do.

I need a mechanism to read List of HashMap data and populate in HTML Table.

Thanks in advance

eis
  • 51,991
  • 13
  • 150
  • 199
Manu
  • 57
  • 1
  • 10
  • 1
    This is unclear. It sounds like your question is actually "how do I produce JSON from Java?". – Oliver Charlesworth Jul 11 '17 at 10:31
  • What happens is you most likely not sending values in right format. We don't know if they really are, but `value` of your `` looks like stock `toString()` on a Java list, which isn't really helpful. – M. Prokhorov Jul 11 '17 at 10:52

1 Answers1

2

Just write JSON from Java yourself. You don't need extra dependencies to do that if you don't want to, just write your hashmap like { "key":"value","key2":"value2" } etc. You can see a more detailed JSON example in JSON.org, for instance.

One option to do that is to define a JSONMap class that has one final variable of type HashMap, and on toString() of your class you'd create JSON output from that hashmap.

Also, you can just output JSON to a javascript variable like var yourVariable = { "key":"value","key2":"value2" };, no need for hidden input unless you need to submit the data as part of a form submit.

On converting json data to an HTML table, see this discussion.

eis
  • 51,991
  • 13
  • 150
  • 199
  • I used the objectmapper in java and converted my list to string, which will have json format, but the problem here is while we perform json.parse in javascript, we support IE 5 browser its giving error ''json' is undefined' – Manu Jul 11 '17 at 12:55
  • @Manu IE5? Seriously, all the way from 1999? You do know that IE 11 is the current version of IE, and even that has been superseded by Edge? But ok, for IE older than IE8, you need [JSON.js](https://github.com/douglascrockford/JSON-js). – eis Jul 11 '17 at 18:26
  • hehehe cannot help it, its our requirement our customers still use IE 5 – Manu Jul 12 '17 at 06:39
  • @Manu note though that JSON.parse is meant for parsing JSON (javascript object notation) from string variable containing JSON. Since you are able to write directly as javascript object notation, you don't need to use JSON.parse: you can just write already-parsed object. – eis Jul 12 '17 at 09:50