1

I am trying to convert my JSON String to Object but the object is coming null. Somehow I am missing something while deserializing the JSON String.

My JSON String is:

{
  "destination_addresses": [
    "Bhusawal, Maharashtra, India"
  ],
  "origin_addresses": [
    "Pune, Maharashtra, India"
  ],
  "rows": [
    {
      "elements": [
        {
          "distance": {
            "text": "396 km",
            "value": 395581
          },
          "duration": {
            "text": "8 hours 20 mins",
            "value": 29981
          },
          "status": "OK"
        }
      ]
    }
  ],
  "status": "OK"
}`

My Formatter class is something like below:

    public class JSON2Apex {

    public  JSON2Apex() {
         List<String> destination_addresses;
         List<String> origin_addresses;
         List<Rows> rows;
         String status;
    }

    public class Elements {
        public Distance distance;
        public Distance duration;
        public String status;
    }

    public class Distance {
        public String text;
        public Integer value;
    }

    public class Rows {
        public List<Elements> elements;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

Below is how I am converting my JSON to Object:

JSON2Apex JSONDeserialized = (JSON2Apex)JSON.deserialize(jsonResults, JSON2Apex.class);
4
  • I don't think the json returned is a list of your JSON2Apex class but only an instance of it. Like in the parse method of your JSON2Apex class. – Martin Lezer Feb 12 '18 at 9:55
  • Even If I try without list, its coming null. I'll update the same – Samir Feb 12 '18 at 9:57
  • Is JSONDeserialized null or one of its attributes? Please edit your question to be more clear. As written it indicates the former but that should be impossible. I think you meant the latter. – Adrian Larson Feb 12 '18 at 12:56
  • When I was seeing JSONDeserialized in debug, it was coming null. When I moved the attributes out of constructor, it worked @AdrianLarson – Samir Feb 12 '18 at 13:30
2

Problem was with my JSON Class. I was initializing in the constructor.

public class JSON2Apex {
    //moved this out of constructor
    List<String> destination_addresses;
    List<String> origin_addresses;
    List<Rows> rows;
    String status;


    public class Elements {
        public Distance distance;
        public Distance duration;
        public String status;
    }

    public class Distance {
        public String text;
        public Integer value;
    }

    public class Rows {
        public List<Elements> elements;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}
1
  • 1
    Come on, man! You've been here long enough that you should know how to format as a code block. Moving your variables out of the constructor worked because doing so made them class variables (as opposed to local variables that were only scoped to your constructor). When these variables were declared in your constructor, you were probably still getting a JSON2Apex instance back, but with no class variables, this object wouldn't actually store anything. – Derek F Feb 12 '18 at 14:04

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.