Parsing JSON Object in Salesforce Apex

后端 未结 1 1366
一生所求
一生所求 2021-02-15 10:13

How do I parse a jsonObject which is in a given format in Apex?

I need List from the items array which contains the id attribute. What is some appropriate met

相关标签:
1条回答
  • 2021-02-15 10:59

    You can use the deserializeUntyped function to work with the JSON in plain object/list/map objects. You just need to remember to cast everything to the expected type (exactly as you would in Java). So to access the id field of each item, you'd do

    public void parse() {
        Map<String, Object> root = (Map<String, Object>)JSON.deserializeUntyped(getJsonToParse());
        List<Object> items = (List<Object>)root.get('items');
        for (Object item : items) {
            Map<String, Object> i = (Map<String, Object>)item;
            System.debug(i.get('id'));
        }
    }
    

    which generates this debug output:

    Debug log results

    0 讨论(0)
提交回复
热议问题