Creating JSON in Flex

In the Flex JSON Portlet that i blogged about yesterday, i was submitting values submitted by users for First Name, Last Name and email as form parameters, which seems natural.

Then i thought lets see if i can create a Action Script class and then convert it into JSON and submit it as JSON String to server side, so i did create my First Action Script Class like this

I used Flash Builder 4.0 for creating this class, All i had to do was to right click and say create Action Script Class and it asked me for package and class name like Java and created this class then after adding variables in it i did select each of them and said Generate Geters and setters, it marked that variable as private first and generated methods like this

package com.webspherenotes.flex
{
public class Contact
{
private var _firstName:String;
private var _lastName:String;
private var _email:String;

public function Contact()
{
}
public function get email():String
{
return _email;
}
public function set email(value:String):void
{
_email = value;
}
public function get lastName():String
{
return _lastName;
}
public function set lastName(value:String):void
{
_lastName = value;
}
public function get firstName():String
{
return _firstName;
}
public function set firstName(value:String):void
{
_firstName = value;
}
}
}


After that i thought lets create ContactDTO which will be used for submitting data from client side to server side so i created a class like this

package com.webspherenotes.flex
{
public class ContactDTO
{
private var _contact:Contact;
public function ContactDTO()
{
}
public function get contact():Contact
{
return _contact;
}
public function set contact(value:Contact):void
{
_contact = value;
}

}
}


Then i made changes in the event listener for the Add Employee button so that it creates object of Contact and ContactDTO and then makes use of JSON.encode() method to convert that object into JSON String and submitted that string as a value of myContactStr

public function addContact():void{
trace("Entering useHttpService");
service = new HTTPService();
service.url = nextActionURL;
service.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
service.method = "POST";

var myContact:Contact =new Contact();
myContact.firstName = txtFirstName.text;
myContact.lastName = txtLastName.text;
myContact.email = txtEmail.text;
var myContactDTO:ContactDTO = new ContactDTO();
myContactDTO.contact = myContact;
var myContactJSONStr:String = JSON.encode(myContactDTO);

trace("Submitting MyContactStr " + myContactJSONStr);
var params:Object = { myContactStr: myContactJSONStr };
service.send(params);
getContactList();
trace("Entering useHttpService");
} }


Now when i try to add a new contact, the flex code creates a JSON string like this and submits it as form parameter

{"contact":{"firstName":"James","lastName":"bond","email":"jamesbond@mi5.com"}}


Then on the server side i am reading value of myContactStr form parameter instead of firstName, lastName and email and then using XStream to parse that string into Contact object with code like this

public static Contact convertJSONToJava(String jsonStr){
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("contact", Contact.class);
System.out.println("setting values of contact object");
Contact contact= (Contact)xstream.fromXML(jsonStr);
return contact;
}

No comments: