2 Replies Latest reply on Nov 1, 2010 11:14 AM by eric_day

    String to JSON

    VirtualCoder Level 1

      Folks,

       

      How do I convert a string containing "{[ {key:"value"},...]}" into json.

      I have tried json.encode function from the core libraries but nothing seems to work.

       

      The problem persists if in action script I put everything withing quotes, such as myvar:String = "{...}"

      All works just fine if I write it as myvar:String = {...}

       

      However, without strings, it is next to impossible for me to construct more complex concationations.

       

      Thanks again,

      VC

        • 1. Re: String to JSON
          rootsounds Level 4

          JSON.encode(o:Object) returns a String representation of the given Object.

          JSON.decode(s:String) parses the given String into a native Object.

           

          It sounds like what you are doing is manually building a JSON string. It is JSON. No conversion required. However, it may be easier and  more useful to construct an object and encode it into a JSON string.

          • 2. Re: String to JSON
            eric_day

            Although the answer from rootsounds is probably useful, I'll just point out that you can make that string compile in AS3 by either using single quotation marks to delimit the entire string:

             

                 var mystr:String = '{[ {key:"value"},...]}';

             

            Or by prepending each internal double quotation mark with an escape character:

             

                 var mystr:String = "{[ {key:\"value\"},...]}";

             

             

            Thanks,

            Eric

            Flex SDK Developer