4 Replies Latest reply on May 27, 2011 3:42 PM by blazejewicz

    Need help with JSON.decode

    BigZ2011 Level 1

      I'm getting a JSON query response that has about 4 layers deep worth of nested objects that I need to drill into. The JSON.decode examples I'm finding online are very simple and use and older syntax than what's supported in Flex 4.5.

       

      I was hoping someone here could give me a quick tip or two for pulling the "html_instructions" values from a JSON result like this:

       

      {
        "status": "OK",
        "routes": [ {
          "summary": "US-97 S and I-5 S",
          "legs": [ {
            "steps": [ {
              "travel_mode": "DRIVING",
              "start_location": {
                "lat": 45.6692700,
                "lng": -118.8421500
              },
              "end_location": {
                "lat": 45.6697700,
                "lng": -118.8456500
              },
              "polyline": {
                "points": "}wvuGljjtUJ`@DjBuBlP",
                "levels": "B??B"
              },
              "duration": {
                "value": 26,
                "text": "1 min"
              },
              "html_instructions": "Head \u003cb\u003esouthwest\u003c/b\u003e on \u003cb\u003eMcKennon Rd\u003c/b\u003e toward \u003cb\u003eSW Bartsch Rd/Co 1304 Rd\u003c/b\u003e",
              "distance": {
                "value": 284,
                "text": "0.2 mi"
              }
            }, {
              "travel_mode": "DRIVING",
              "start_location": {
                "lat": 45.6697700,
                "lng": -118.8456500
              },
              "end_location": {
                "lat": 45.6700400,
                "lng": -118.8466500
              },
              "polyline": {
                "points": "a{vuGh`ktUWrAIjASf@",
                "levels": "B??B"
              },
              "duration": {
                "value": 10,
                "text": "1 min"
              },
              "html_instructions": "Continue straight onto \u003cb\u003eSW Bartsch Rd/Co 1304 Rd\u003c/b\u003e",
              "distance": {
                "value": 85,
                "text": "279 ft"
              }
            }

      }
        } ]
      }

       

      Many, many thanks in advance!!!

        • 1. Re: Need help with JSON.decode
          blazejewicz Level 4

          Hi,

          As i'm not sure what api you're using to decode json: Once you convert it to object info you're looking for is accessible as:

          json.routes[0].legs[0].steps[0].html_instructions

          or:

          json.routes[N].legs[N].steps[N].html_instructions

          when N is length of given array and "json" is top most object from loaded data,

           

          kind regards,

          Peter

          • 2. Re: Need help with JSON.decode
            BigZ2011 Level 1

            Thank you very much for your help, Peter!

            • 3. Re: Need help with JSON.decode
              BigZ2011 Level 1

              I realize this is a dumb question but maybe the coffee just hasn't kicked in yet.  I"m trying to loop through the JSON reponse and came up with the following statement:

               

              for (i = 0; i< manager.routes[0].legs[0].steps[i].html_instructions.length; i++)
              {
              TxtAreaDirections.text += manager.routes[0].legs[0].steps[i].html_instructions.toString() + "\n\n";
              }

               

               

              I'm getting all the results I need but I'm also getting:

               

              TypeError: Error #1010: A term is undefined and has no properties.

               

              I can't figure out what's wrong with my loop. Any help is much appreciated!

              • 4. Re: Need help with JSON.decode
                blazejewicz Level 4

                Hi,

                 

                you need to be careful what is used as source for your loops conditions, for example "html_instructions" is String adn I think it was not your intent to use it as array-like accessing counter. It would rather be I think:

                var manager:Object = com.brokenfunction.json.decodeJson(data as ByteArray);

                var steps:Array = manager.routes[0].legs[0].steps;

                for(var i:Number = 0; i < steps.length; i++)

                {

                     var step:Object = steps[i];

                     if(step.hasOwnProperty("html_instructions"))

                     {

                          trace(step.html_instructions);

                     }

                }

                hth

                kind regards,

                Peter