I have a JSON string that I get in a response. Example:

{"Place":[{"datetime":"2011-03-11 04:46:23","depth":24.4,"lng":142.369,"lat":38.322}]}

I get the JSON values in the controller and put it in a list that will be used in VF page:

public List<Place> place

In VF page I use the to display the values:

<apex:dataTable value="{!places}" var="var">
  <apex:column >
    <apex:facet name="header">DateTime</apex:facet>
    <apex:outputText value="{!var.DateTime}"/>
  <apex:column >
    <apex:facet name="header">Depth</apex:facet>
    <apex:outputText value="{!var.depth}"/>  
  <apex:column >
    <apex:facet name="header">Longitude</apex:facet>
    <apex:outputText value="{!var.lng}"/>
  <apex:column >
    <apex:facet name="header">Longitude</apex:facet>
    <apex:outputText value="{!var.lat}"/>

I get all the values in the table except for one column i.e. datetime. It is of type String in the Class Place. I am not sure why this String - "2011-03-11 04:46:23" would not appear in the table. Any suggestions?

Root Cause I found:

In my apex Class Place , I have a variable declared to store datetime-> String adateTime ; since I can't use dateTime as variable name as it is reserved.

The JSON result has "datetime":"2011-03-11 04:46:23". Hence the value is null.

  • 2
    Maybe change the case of your key to match your index in the json? – Ronnie Oct 1 '17 at 0:20
  • 2
    P.S. DateTime is a reserved keyword, and will not work in future versions. If you can change your JSON, you should do so. Also, JSON parsing is case sensitive, as mentioned by Ronnie. – sfdcfox Oct 1 '17 at 0:22
  • Just added the root cause in the question and you commented the same :) . I cannot change the JSON as it is coming from third party. What option do I have here? – SfdcBat Oct 1 '17 at 0:24
  • maybe rename the key? – glls Oct 1 '17 at 0:36
  • Yes , I looking at that option. May be I should use - JSONParser class - developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – SfdcBat Oct 1 '17 at 0:40

You can use JSON.deserializeUntyped to get at the data. Here's an exec anon example that demonstrates this:

String body = '{"Place":[{"datetime":"2011-03-11 04:46:23","depth":24.4,"lng":142.369,"lat":38.322}]}';
Map<String, Object> placeObject = (Map<String, Object>)JSON.deserializeUntyped(Body);
List<Object> placeObjectList = (List<Object>)placeObject.get('Place');
for(Object placeValueItem: placeObjectList) {
    Map<String, Object> placeValue = (Map<String, Object>)placeValueItem;

I find that this is marginally easier than dealing with JSONParser, as you get native objects to work with. Note that DateTime values will need to be parsed if you want them as anything other than a string.

  • Much simpler. I was going to use JSONParser and recreate the JSON string with expected tags but that looks like lot of processing time. – SfdcBat Oct 1 '17 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.