I have an array of objects like
var word_list = [
{text: "halo", weight: 2, link:"a"},
{text: "hey", weight: 3, link:"a"},
{text: "cat", weight: 2, link:"a"},
{text: "dog", weight: 4, link:"a"},
];
I want to use ajax to receive this var from Controller:
function get(){
$.ajax({
type:"post",
url:"readTagCloud",
contentType: 'application/json'
datatype : "json",
success:function(data) {
word_list = data;
},
error: function() {}
});
}
code of Controller:
@RequestMapping("readTagCloud")
@ResponseBody
public String readTagCloud(String topicId,Integer emotion){
JSONArray words = new JSONArray();
for(int i = 0;i<10;i++){
JSONObject word = new JSONObject();
try {
word.put("text", i+"");
word.put("weight", i/2);
word.put("link", "none");
} catch (JSONException e) {
e.printStackTrace();
}
words.put(word);
}
return words.toString();
}
But,when ajax work,it make the word_list become String type.I want to know how to read JSONArray in Spring MVC