function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dippan PatelDippan Patel 

json serialize list

Hi, 

I have a list with the following format in apex controller. 

Custom__Object_Analysis__c:{Id=a0L1N00000L1izqUAB, Name=All Accounts, CreatedDate=2018-02-15 15:15:12, isCustom__c=false, Fields__c=0, Filter__c=Industry = '', Object_Name__c=account, Tally__c=0}

How do I serialize this? 
JSON.Serialize() gives Uncaught SyntaxError: Unexpected string 
Best Answer chosen by Dippan Patel
v varaprasadv varaprasad
Hi Dippan,
Try like below : 

String s = JSON.serialize(proposal);

 
Product product = new Product();

product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string output = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "ExpiryDate": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 

All Answers

v varaprasadv varaprasad
Hi Dippan,
Try like below : 

String s = JSON.serialize(proposal);

 
Product product = new Product();

product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string output = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "ExpiryDate": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
This was selected as the best answer
Raj VakatiRaj Vakati
This is the correcct JSON 
 
{Id=a0L1N00000L1izqUAB, Name=All Accounts, CreatedDate=2018-02-15 15:15:12, isCustom__c=false, Fields__c=0, Filter__c=Industry = '', Object_Name__c=account, Tally__c=0}

 
Raj VakatiRaj Vakati
This is the simple way 
 
Custom__Object_Analysis__c proposal = [Select Id ,Name from Custom__Object_Analysis__c limit 1 ] ;
String s = JSON.serialize(proposal);