Is there any function to deserialize an JSON to an array in lightning component?

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);
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 2

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

<aura:unescapedHtml value=""/>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x