help with JSON method

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
_ex_
Posts: 9
Joined: 14 Jun 2007, 15:19

help with JSON method

Post by _ex_ »

Hi there!
I'm new to SFS and JSON and I was trying to make a simple method to return the triples of the values sent on an array but for some reason it's failing (the JSON object). Here my method

Code: Select all

	public void handleRequest(String cmd, JSONObject jso, User u, int fromRoom) {
		
		trace("The command -> " + cmd + " was invoked by user -> " + u.getName());
		if (cmd.equals("triple")) {
			// Main response object
			JSONObject response = new JSONObject();
			
			// Set the command name for the response
			response.put("_cmd", "triple");
			
			// Cycle through all items
			JSONArray arr = jso.getJSONArray("values");
			
			// Nested array object for return
			JSONArray returnArray = new JSONArray();
			
			for (int i = 0; i < arr.length(); i++) {
				// Trace to server console
				System.out.println("Item " + i + " = " + arr.getInt(i));
				returnArray.put(3*arr.getInt(i));
			}
			
			// Add returnArray to main response object
			response.put("values", returnArray);

			// Prepare a list of recipients and put the user that requested the command
			LinkedList recipients = new LinkedList();
			recipients.add(u.getChannel());
			 
			// Send data to client
			sendResponse(response, fromRoom, u, recipients);
		}
	}
must be something really obvious to someone with JSON experience any ideas?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

What is the problem? What error do you get?
Lapo
--
gotoAndPlay()
...addicted to flash games
_ex_
Posts: 9
Joined: 14 Jun 2007, 15:19

Post by _ex_ »

Hi Lapo:

The problem is that the code doesn't compile.
I added de json import and I'm using the json.jar of the sfs installation.
The problem seems is on the put() method.
Do you have some example code for using json with java code? Your examples using XML were really helpfull.

Thanks.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

try checking the JsonArray help -> http://www.json.org/javadoc/org/json/JSONArray.html
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply