2013-02-16 13 views
8

Come analizzare un jsonObject che si trova in un formato specificato in Apex?Analisi dell'oggetto JSON in Salesforce Apex

Ho bisogno della Lista < String> dall'array items che contiene l'attributo id. Qual è un metodo appropriato?

Per analizzarlo, cerco di creare una classe con il codice:

public class JSON2Apex { 

    public class Items { 
     public String kind; 
     public String etag; 
     public String id; 
     public String status; 
     public String htmlLink; 
     public String created; 
     public String updated; 
     public String summary; 
     public String description; 
     public String location; 
     public Creator creator; 
     public Creator organizer; 
     public Start start; 
     public Start end; 
     public String iCalUID; 
     public Integer sequence; 
     public Reminders reminders; 
    } 

    public class Reminders { 
     public Boolean useDefault; 
    } 

    public class Start { 
     public String dateTime; 
    } 

    public class Creator { 
     public String email; 
     public String displayName; 
     public Boolean self; 
    } 

    public String kind; 
    public String etag; 
    public String summary; 
    public String updated; 
    public String timeZone; 
    public String accessRole; 
    public List<DefaultReminders> defaultReminders; 
    public List<Items> items; 

    public class DefaultReminders { 
     public String method; 
     public Integer minutes; 
    } 

} 

Ma in questa classe si sta dimostrando una certa parola chiave riservata è facile come fine, dateTime ecc in modo non può procedere oltre con questo approccio .

{ 
    "kind": "calendar#events", 
    "etag": "\"ZrhdJMCgpoUK_a5fT7XOC6xn46g/jAScaUzsyk9nbyyvTc1Wa3-tulA\"", 
    "summary": "[email protected]", 
    "updated": "2013-02-16T16:23:20.057Z", 
    "timeZone": "Asia/Calcutta", 
    "accessRole": "owner", 
    "defaultReminders": [ 
     { 
      "method": "email", 
      "minutes": 10 
     }, 
     { 
      "method": "popup", 
      "minutes": 10 
     } 
    ], 
    "items": [ 
     { 
      "kind": "calendar#event", 
      "etag": "\"ZrhdJMCgpoUK_a5fT7XOC6xn46g/Z2NhbDAwMDAxMzU2MTQ1NTg1NjYxMDAw\"", 
      "id": "ngo4lfq6q7c4dm8bej7m47e5lc", 
      "status": "confirmed", 
      "htmlLink": "https://www.google.com/calendar/event?eid=bmdvNGxmcTZxN2M0ZG04YmVqN200N2U1bGMgcml0ZXNobWVoYW5kaXJhdHRhQG0", 
      "created": "2012-12-22T03:04:01.000Z", 
      "updated": "2012-12-22T03:06:25.661Z", 
      "summary": "hello", 
      "description": "hgjgjh", 
      "location": "hkhkhk", 
      "creator": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "organizer": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "start": { 
       "dateTime": "2013-01-01T10:00:00+05:30" 
      }, 
      "end": { 
       "dateTime": "2013-01-15T00:00:00+05:30" 
      }, 
      "iCalUID": "[email protected]", 
      "sequence": 1, 
      "reminders": { 
       "useDefault": true 
      } 
     }, 
     { 
      "kind": "calendar#event", 
      "etag": "\"ZrhdJMCgpoUK_a5fT7XOC6xn46g/Z2NhbDAwMDAxMzYxMDMxNTg1OTYzMDAw\"", 
      "id": "oap7nr7ukjug6euo24kvhmfbm0", 
      "status": "confirmed", 
      "htmlLink": "https://www.google.com/calendar/event?eid=b2FwN25yN3VranVnNmV1bzI0a3ZobWZibTAgcml0ZXNobWVoYW5kaXJhdHRhQG0", 
      "created": "2013-02-16T16:19:45.000Z", 
      "updated": "2013-02-16T16:19:45.963Z", 
      "summary": "Hello World", 
      "creator": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "organizer": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "start": { 
       "dateTime": "2013-02-11T11:00:00+05:30" 
      }, 
      "end": { 
       "dateTime": "2013-02-11T12:00:00+05:30" 
      }, 
      "iCalUID": "[email protected]", 
      "sequence": 0, 
      "reminders": { 
       "useDefault": true 
      } 
     }, 
     { 
      "kind": "calendar#event", 
      "etag": "\"ZrhdJMCgpoUK_a5fT7XOC6xn46g/Z2NhbDAwMDAxMzYxMDMxNjQxMDYzMDAw\"", 
      "id": "k2mvqecqdgoudt6fl4e5hhqna0", 
      "status": "confirmed", 
      "htmlLink": "https://www.google.com/calendar/event?eid=azJtdnFlY3FkZ291ZHQ2Zmw0ZTVoaHFuYTAgcml0ZXNobWVoYW5kaXJhdHRhQG0", 
      "created": "2013-02-16T16:20:41.000Z", 
      "updated": "2013-02-16T16:20:41.063Z", 
      "summary": "Hello Wol", 
      "creator": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "organizer": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "start": { 
       "dateTime": "2013-02-11T13:00:00+05:30" 
      }, 
      "end": { 
       "dateTime": "2013-02-11T14:00:00+05:30" 
      }, 
      "iCalUID": "[email protected]", 
      "sequence": 0, 
      "reminders": { 
       "useDefault": true 
      } 
     }, 
     { 
      "kind": "calendar#event", 
      "etag": "\"ZrhdJMCgpoUK_a5fT7XOC6xn46g/Z2NhbDAwMDAxMzYxMDMxNjUyNDE1MDAw\"", 
      "id": "0n2if07oo9pvfdnf7f0a6cldns", 
      "status": "confirmed", 
      "htmlLink": "https://www.google.com/calendar/event?eid=MG4yaWYwN29vOXB2ZmRuZjdmMGE2Y2xkbnMgcml0ZXNobWVoYW5kaXJhdHRhQG0", 
      "created": "2013-02-16T16:20:52.000Z", 
      "updated": "2013-02-16T16:20:52.415Z", 
      "summary": "Hello World1", 
      "creator": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "organizer": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "start": { 
       "dateTime": "2013-02-11T15:30:00+05:30" 
      }, 
      "end": { 
       "dateTime": "2013-02-11T16:30:00+05:30" 
      }, 
      "iCalUID": "[email protected]", 
      "sequence": 0, 
      "reminders": { 
       "useDefault": true 
      } 
     }, 
     { 
      "kind": "calendar#event", 
      "etag": "\"ZrhdJMCgpoUK_a5fT7XOC6xn46g/Z2NhbDAwMDAxMzYxMDMxNzcwNDMzMDAw\"", 
      "id": "r365lrv775bqjiplmaqjro9grc", 
      "status": "confirmed", 
      "htmlLink": "https://www.google.com/calendar/event?eid=cjM2NWxydjc3NWJxamlwbG1hcWpybzlncmMgcml0ZXNobWVoYW5kaXJhdHRhQG0", 
      "created": "2013-02-16T16:22:50.000Z", 
      "updated": "2013-02-16T16:22:50.433Z", 
      "creator": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "organizer": { 
       "email": "[email protected]", 
       "displayName": "RITESH MEHANDIRATTA", 
       "self": true 
      }, 
      "start": { 
       "dateTime": "2013-02-11T14:30:00+05:30" 
      }, 
      "end": { 
       "dateTime": "2013-02-11T15:30:00+05:30" 
      }, 
      "iCalUID": "[email protected]", 
      "sequence": 0, 
      "reminders": { 
       "useDefault": true 
      } 
     } 
    ] 
} 

risposta

19

È possibile utilizzare la funzione deserializeUntyped di lavorare con il JSON negli oggetti oggetto/list/mappa piana. Devi solo ricordarti di trasmettere tutto al tipo atteso (esattamente come faresti in Java). Quindi, per accedere al campo id di ogni elemento, faresti

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')); 
    } 
} 

che genera questo output di debug:

Debug log results

+0

grazie mille per la risposta –

+0

hey invitiamo a rispondere a questa query http: //stackoverflow.com/questions/14921235/debugging-schedulable-job-in-apex-salesforce –

+0

@superfell - Sei fantastico! La tua risposta mi ha fatto uscire da un sottaceto! Grazie! –