Deserialization of JSON from APIs

PHOTO EMBED

Fri May 12 2023 15:49:37 GMT+0000 (Coordinated Universal Time)

Saved by @gbritgs #apex

public static List<TestClass> getResponse() {
        //HttpResponse resp = 'callout';
        List<TestClass> lst = (List<TestClass>) JSON.deserialize(resp.getBody(),List<TestClass>.class);

  return lst;
    }

    public class TestClass {
        public String code;
    }
content_copyCOPY

Attention to the way the response is being constructed because it will influence in your deserialize: An array of objects will need a cast of (List<sObject>) (ex: line 3) A simple JSON will use a simpler cast