Monday, June 22, 2009

JSONArray example

Here is how to create a JSONArray on the Java side:
public JSONObject onNameChangeEvent(String name)
{
Share share = shareDAO.getShareByName(name);

JSONArray JSONAttributes = new JSONArray();
JSONAttributes.put(name);
JSONAttributes.put("Market Price");
JSONAttributes.put(share.getPrice());
JSONAttributes.put("Market Year");
JSONAttributes.put(share.getPriceYear());
JSONAttributes.put("Average Growth(%)");
JSONAttributes.put(share.getGrowthPercent());

JSONObject json = new JSONObject();
json.put("attributes", JSONAttributes);
return json;
}


Here is how to use it in the javascript side:

function onNameSelectFunction(response)
{
$('details').update(
"<strong>" + response.attributes[0] + "</strong>"
+ "<br/>"
+ response.attributes[1] + " : " + response.attributes[2]
+ "<br/>"
+ response.attributes[3] + " : " + response.attributes[4]
+ "<br/>"
+ response.attributes[5] + " : " + response.attributes[6]
+ "<br/>"
+ "<br/>"
);
}

No comments:

Post a Comment