1

I have been using JSON.parse() function to deserialize JSON object, It is not working for special characters. For example, if the JSON contains a date it being converted as 2012/04/18 instead of 2012/04/18. As per the info in this site https://success.salesforce.com/issues_view?id=a1p30000000wkAIAAY there is a function $A.util.json.encode() to serialize. I'm searching for function to deserialize a JSON in controllerJS.

({
Search :  function(component, event, helper) {
    var searchtext  = component.find("searchtext").get("v.value");
    var model       = component.find("Model").get("v.value");
    var brand       = component.get("v.Brand");
    var subcategory = component.get("v.SubCategory");
    var category    = component.get("v.Category");
    var page    = component.get("v.pageIndex");
    var Size    = component.get("v.pageSize");

    var action = component.get("c.KnowledgeDocs");
    action.setParams({"brand" : brand,
                      "model" : model,
                      "category" : category,
                      "subcategory" : subcategory,
                      "searchText" : searchtext,
                      "pageNumber" : page,
                      "pageSize" : Size});
    action.setCallback(this,function(result){
        var responses = result.getReturnValue();
        if(component.isValid() && result.getState() === "SUCCESS"){
            var response = JSON.parse( responses );
            console.log($A.util.json.encode( response ));
            component.set('v.documents',response.documents);
            component.set('v.totalRecords',response.totalRecords);
            component.set('v.totalPages',response.totalPages);
            component.set('v.pageIndex',response.pageNumber);
        }
    });
    $A.enqueueAction(action);
} 
}) 

Apex Class

@AuraEnabled
public static string KnowledgeDocs(string brand, string model, string category, string subcategory, string searchText, integer pageNumber, integer pageSize){
    CCService.PublicationSearchRequest searchParams = new CCService.PublicationSearchRequest();

        searchParams.Attributes.add(
            new CCService.Attributes(CCService.FILTER_CRITERIA_BRAND, brand, eqCondition.name()));
        searchParams.Attributes.add(
            new CCService.Attributes(CCService.FILTER_CRITERIA_MODEL, model, eqCondition.name()));
        searchParams.Attributes.add(
            new CCService.Attributes(CCService.FILTER_CRITERIA_CATEGORY, category, eqCondition.name()));
        searchParams.Attributes.add(
            new CCService.Attributes(CCService.FILTER_CRITERIA_SUBCATEGORY, subcategory, eqCondition.name()));
    }

    searchParams.Attributes.add(
        new CCService.Attributes(CCService.FILTER_CRITERIA_SEARCH, null, containsCondition.name(), searchText, 'OR'));

   /*dosearch makes an rest call and returns body from the response*/

    CCService.PublicationSearchContent publications = CCService.doSearch(searchParams);
    system.debug('===>'+publications);
    return Json.serialize(publications);
}
3
  • What does your Apex controller look like? What does KnowledgeDocs() return?
    – Folkert
    Feb 17 '17 at 12:14
  • @Folkert KnowledgeDocs() returns a JSON string.
    – Venky
    Feb 17 '17 at 12:58
  • Can you post the code for the controller as well? Does it serialize the JSON based on an Apex Class or did you build it yourself using string concatenation?
    – Folkert
    Feb 17 '17 at 12:59
1

You can take a look on System.JSON class that has various methods for serializing and deserializing. (salesforce also has other kinds of json support). You might need a round trip to server though.

1
  • thanks for quick response, but do they work in ControllerJS of the lightning component as in Apex Class. Please take a look at my code just updated my post.
    – Venky
    Feb 17 '17 at 12:01
0

By using this component I'm getting the required output.

<aura:unescapedHtml value=""/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.