Q:

Deserialize JSON to Apex class

I have the following JSON
[{"Name":"Global Media","Phone":"(905) 555-1212","Id":"0016A0000035QXuQAM","Contacts":[{"AccountId":"0016A0000035QXuQAM","Id":"0036A000002BnDpQAK","LastName":"White","FirstName":"Carole"},{"AccountId":"0016A0000035QXuQAM","Id":"0036A000002BnDqQAK","LastName":"Amos","FirstName":"Jon"},{"AccountId":"0016A0000035QXuQAM","Id":"0036A000002BnDoQAK","LastName":"Minor","FirstName":"Geoff"}]},{"Name":"Acme","Phone":"(212) 555-5555","Id":"0016A0000035QXvQAM","Contacts":[{"AccountId":"0016A0000035QXvQAM","Id":"0036A000002BnDsQAK","LastName":"Jones","FirstName":"Howard"},{"AccountId":"0016A0000035QXvQAM","Id":"0036A000002BnDtQAK","LastName":"Tomlin","FirstName":"Leanne"},{"AccountId":"0016A0000035QXvQAM","Id":"0036A000002BnDrQAK","LastName":"Stamos","FirstName":"Edward"}]},{"Name":"salesforce.com","Phone":"(415) 901-7000","Id":"0016A0000035QXwQAM","Contacts":[{"AccountId":"0016A0000035QXwQAM","Id":"0036A000002BnDuQAK","LastName":"Benioff","FirstName":"Marc"}]},{"Name":"ravi","Phone":"8977007142","Id":"0016A00000AmKRyQAN","Contacts":[{"AccountId":"0016A00000AmKRyQAN","Id":"0036A00000FWoavQAD","LastName":"reddy","FirstName":"Ravikumar"}]},{"Name":"ins","Id":"0016A00000Apv84QAB","Contacts":[{"AccountId":"0016A00000Apv84QAB","Id":"0036A00000FbtK4QAJ","LastName":"arjun"}]},{"Name":"ims","Rating":"Hot","Id":"0016A00000ApBGDQA3","Contacts":[{"AccountId":"0016A00000ApBGDQA3","Id":"0036A00000FbdpaQAB","LastName":"aru"}]},{"Name":"ims1","Id":"0016A00000DHZdGQAX","Contacts":[{"AccountId":"0016A00000DHZdGQAX","Id":"0036A00000FceFPQAZ","LastName":"malli"}]},{"Name":"ims","Phone":"8977007142","Rating":"Hot","Id":"0016A00000ApBGhQAN","Contacts":[{"AccountId":"0016A00000ApBGhQAN","Id":"0036A00000FbdpfQAB","LastName":"Malli"}]}]

How can I deserialize it to Apex ?

A:

Use json2apex tool
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public class JSON2Apex {
        public String Name;
        public String Phone;
        public String Id;
        public List<Contacts> Contacts;
        public String Rating;
    }

    public class Contacts {
        public String AccountId;
        public String Id;
        public String LastName;
        public String FirstName;
    }

    public class Contacts_Z {
        public String AccountId;
        public String Id;
        public String LastName;
    }

    public static List<JSON2Apex> parse(String json) {
        return (List<JSON2Apex>) System.JSON.deserialize(json, List<JSON2Apex>.class);
    }
}

and then you can deserialize it in the following way:
List<JSON2Apex> des = JSON2Apex.parse(jsonString);

