Manu
Manu

Reputation: 57

How to read a List of HashMap of java in Javascript

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

Upvotes: 0

Views: 721

Answers (1)

eis
eis

Reputation: 53502

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.

Upvotes: 2

Related Questions