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; = txtEmail.text;
var myContactDTO:ContactDTO = new ContactDTO(); = myContact;
var myContactJSONStr:String = JSON.encode(myContactDTO);

trace("Submitting MyContactStr " + myContactJSONStr);
var params:Object = { myContactStr: myContactJSONStr };
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


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.alias("contact", Contact.class);
System.out.println("setting values of contact object");
Contact contact= (Contact)xstream.fromXML(jsonStr);
return contact;


srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

EG MEDI said... is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery

Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store