Parsing JSON Object in Salesforce Apex

后端 未结 1 1362
一生所求
一生所求 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 root = (Map)JSON.deserializeUntyped(getJsonToParse());
        List items = (List)root.get('items');
        for (Object item : items) {
            Map i = (Map)item;
            System.debug(i.get('id'));
        }
    }
    
    
    

    which generates this debug output:

    Debug log results

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