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
nununinununi 

Serialize object to JSON format

I am trying to serialize my account object to a JSON format which needs to be something like this:
[
    {
        "AccountId": 1,
        "AccountName": "Dixit company account",
        "AddressBook": [
            {
                "Id": 1,
                "Name": "John Smith",
                "Attn": "Name",
    "StreetName": "123/12 King St",
                "CityName": "Melbourne",
                "State": "VIC",
                "PostCode": 3000,
                "Tel": "(000)- 000-0000",
                "Favorite": true,
                "AccountId": 1
            },
            {
                "Id": 2,
                "Name": "Samantha Parker",
                "Attn": "Name",
    "StreetName": "123/12 King St",
                "CityName": "Melbourne",
                "State": "VIC",
                "PostCode": 3000,
                "Tel": "(000)- 000-0000",
                "Favorite": true,
                "AccountId": 1
            },
            {
                "Id": 3,
                "Name": "Cameron Adams",
                "Attn": "Name",
    "StreetName": "123/12 King St",
                "CityName": "Melbourne",
                "State": "VIC",
                "PostCode": 3000,
                "Tel": "(000)- 000-0000",
                "Favorite": false,
                "AccountId": 1
            }
        ]
    },
    {
        "AccountId": 2,
        "AccountName": "Diageo account",
        "AddressBook": [
            {
                "Id": 1,
                "Name": "Lee Fong",
                "Attn": "LF",
    "StreetName": "123/12 King St",
                "CityName": "Melbourne",
                "State": "VIC",
                "PostCode": 3000,
                "Tel": "(000)- 000-0000",
                "Favorite": true,
                "AccountId": 1
            },
            {
                "Id": 2,
                "Name": "Robert Dujardin",
                "Attn": "Name",
    "StreetName": "123/12 King St",
                "CityName": "Paris",
                "State": "Ile",
                "PostCode": 75000,
                "Tel": "(000)- 000-0000",
                "Favorite": false,
                "AccountId": 1
            },
            {
                "Id": 3,
                "Name": "Pavlia Rokaski",
                "Attn": "Pav",
    "StreetName": "123/12 King St",
                "CityName": "Melbourne",
                "State": "VIC",
                "PostCode": 3000,
                "Tel": "(000)- 000-0000",
                "Favorite": false,
                "AccountId": 1
            }
        ]
    },
    {
        "AccountId": 4,
        "AccountName": "Personal",
        "AddressBook": [
            {
                "Id": 1,
                "Name": "Marc Samson",
                "Attn": "Name",
    "StreetName": "123/12 King St",
                "CityName": "Melbourne",
                "State": "VIC",
                "PostCode": 3000,
                "Tel": "(000)- 000-0000",
                "Favorite": true,
                "AccountId": 1
            },
            {
                "Id": 2,
                "Name": "Francis Maher",
                "Attn": "Name",
    "StreetName": "123/12 King St",
                "CityName": "Melbourne",
                "State": "VIC",
                "PostCode": 3000,
                "Tel": "(000)- 000-0000",
                "Favorite": true,
                "AccountId": 1
            },
            {
                "Id": 3,
                "Name": "Fanny Emerson",
                "Attn": "Name",
    "StreetName": "123/12 King St",
                "CityName": "Melbourne",
                "State": "VIC",
                "PostCode": 3000,
                "Tel": "(000)- 000-0000",
                "Favorite": false,
                "AccountId": 1
            }
        ]
    }
]
AshwaniAshwani
Create a class which should seems like:

class Account {

String AccountId;
List<Object>;

// so on accordiong to object types

}

There are few links which will help you:

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_json.htm

https://developer.salesforce.com/page/Getting_Started_with_Apex_JSON