1

vf page

function createRecord()
{
    j$ = jQuery.noConflict();
    var formData = JSON.stringify(jQuery('#form').serialize()); 
    MyCustomController1.createContact(formData); 
}

controller

@RemoteAction
public static String createContact(String strJsonObjResult)
{
   system.debug('strJsonObjResult='+strJsonObjResult);
}

DEBUG OUTPUT:

strJsonObjResult=[{"name":"firstname","value":"xyz"},{"name":"lastname","value":"xyz"},{"name":"email","value":"[email protected]"},{"name":"phone","value":"789456"},{"name":"dob","value":"2013-05-13"}]

//its showing me like so how to deserialize this?

2 Answers 2

1

You can create a class to parse the input, roughly outlined as follows:

public class Item {
    public String name, value;
}
@RemoteAction public static void createContact(String jsonObjResult) {
    // Serialize Items
    Item[] values = (List<Item>)JSON.deserialize(jsonObjResult, List<Item>.class);
    // process items to create contacts
}

Note: This would be a lot easier if you used standard JSON notation:

{ "FirstName": "xyz", "LastName": "xyz", "Email": "[email protected]", "Phone": "789456", "BirthDate": "2013-05-13" }

Then, you could serialize the object directly:

@RemoteAction public static void createContact(String jsonObjResult) {
    Contact record = (Contact)JSON.deserialize(jsonObjResult, Contact.class);
    insert record;
}
2
  • User may not change the JSON notation because entire form is serialized which would only take input tag attribute name and value in an array.
    – Ashwani
    Commented Jan 18, 2017 at 4:58
  • @Ashwani I know that's how they chose to do it, but it'd be easier if they changed the jQuery script to make it easier to process in Apex. I simply offered two different solutions (I'd personally go with the latter, because they don't have to deal with Date vs. String, etc).
    – sfdcfox
    Commented Jan 18, 2017 at 5:01
0

Try Below piece of code

Parser/Wrapper Class

public class JsonParseToApex{
    public class Class_0 {
        public String name; //firstname
        public String value;//xyz
    }
    public class Class_1 {
        public String name; //lastname
        public String value;//xyz
    }
    public class Class_2 {
        public String name; //email
        public String value;//[email protected]
    }
    public class Class_3 {
        public String name; //phone
        public String value;//789456
    }
    public class Class_4 {
        public String name; //dob
        public String value;//2013-05-13
    }
    public static List<JsonParseToApex> parse(String jsonObjResult){
        JsonParseToApex[] values = (List<JsonParseToApex>)JSON.deserialize(jsonObjResult, List<JsonParseToApex>.class);
        return  values;
    }
}

Method

@RemoteAction public static void createContact(String jsonObjResult) {
    // Call the parse method
    List<JsonParseToApex> wrapperList = JsonParseToApex.parse(jsonObjResult);
    system.debug(wrapperList );
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .