0

I am trying parse the below Json response but the gettext() is returning null and it is not updating fields in Custom object. Can anyone please help me with this ?

Json:

     {
           "results" : [
              {
                 "address_components" : [
                    {
                       "long_name" : "1200",
                       "short_name" : "1200",
                       "types" : [ "street_number" ]
                    }
                 ],
                 "formatted_address" : "abcS, city, state,Country",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.8683573,
                  "lng" : -87.6126831
               },
               "southwest" : {
                  "lat" : 41.86688609999999,
                  "lng" : -87.6146402
               }
            },
            "location" : {
               "lat" : 41.8676217,
               "lng" : -87.6136616
            }, "southwest" : {
                  "lat" : 41.8662727197085,
                  "lng" : -87.61501063029151
               }
            }
         }
      }
   ],
   "status" : "OK"
}

Class:

JSONParser parser = JSON.createParser(res.getBody());
double l1 = null;
double l2 = null;

            while (parser.nextToken() != null) {
                System.debug('entred while');
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                    (parser.getText() == 'location')){

                       parser.nextToken(); 
                       while (parser.nextToken() != JSONToken.END_OBJECT){
                           String txt = parser.getText();
                           parser.nextToken();
                           if (txt == 'lat')
                               l1 = parser.getDoubleValue();
                           else if (txt == 'lng')
                               l2 = parser.getDoubleValue();
                       }

                }
            }

            }
  • I just copied and pasted the while loop and parser from your code and debugged each .getText() call and I get no nulls. The only thing I did differently was paste your minified JSON into the part you are calling res.getBody(). – Jesse Milburn Feb 22 '17 at 20:38
  • @JesseMilburn Thanks for your response. I will try that – Learner Feb 22 '17 at 23:19

Your Answer

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

Browse other questions tagged or ask your own question.