Ezhilan Mahalingam
Ezhilan Mahalingam

Reputation: 3330

Cannot iterate the HashMap converted as JSON String in JSP

How to make the video button always appear when playing videos?
Now the button only appears when we click the video, please help me.
This is my code

In Spring Controller: Returning the Map values as JSON String

Map<String, Long> keyTTLMap = getRedisCacheTTLvalues(filterPattern);

JSONObject jsonObject = new JSONObject();

jsonObject.put("cacheTTLmap", keyTTLMap);

return jsonObject.toString();

In JSP AJAX call:

var json = xmlhttp.responseText;

for ( var i = 0; i < json.cacheTTLmap.length; i++) {
    var obj = json.cacheTTLmap[i];  
    for ( var key in obj) {
        name = key;
        value = obj[key].toString();
        alert("Name "+name +" value "+value);
    }
}

JSON String value:

{
    "cacheTTLmap":
    {
        "product1":81213,    
        "product2":79936
    }   
}

When I try to run my JSP, my JSON string cannot be iterated. Is there any simple way to display HashMap in AJAX, Kindly help me with this. Thanks!

Upvotes: 0

Views: 1339

Answers (1)

Sunny
Sunny

Reputation: 308

var json = '[{"userId" : "123123", "password": "fafafa", "age": "21"}, {"userId" :  
"321321", "password" : "nana123", "age" : "34"}]';

var list = JSON.parse(json);
var output = {};

for(var i=0; i<list.length; i++)
{
    for(var key in list[i])
    {
        if(list[i].hasOwnProperty(key))
        {
            if(typeof output[key] == 'undefined')
            {
                output[key] = [];
            }
            output[key].push(list[i][key]);
        }
    }
}

document.write(JSON.stringify(output));

Upvotes: 1

Related Questions