Loading ...
Sorry, an error occurred while loading the content.

Re: Helpless beginner

Expand Messages
  • jonerlandmadsen
    ... Thank you for your kind answer, but it does not address my problem: How to get the external json array into a text string (that can then be parsed).
    Message 1 of 8 , May 19 7:41 AM
    • 0 Attachment
      --- In json@yahoogroups.com, Stephan Beal <sgbeal@...> wrote:
      >
      > On Thu, May 19, 2011 at 3:47 PM, jonerlandmadsen <
      > jon.erland.madsen@...> wrote:
      >
      > > According to Wikipedia, I can make an object out of it, using "eval". The
      > > example given is:
      > >
      > > var p = eval("(" + contact + ")");
      > >
      > > ..where 'contact' is the json array contained in a text object.
      > >
      > > But there must be a step between, that is not explained.
      > >
      >
      > Nope. JSON is a subset of JavaScript and eval() runs the JavaScript which is
      > passed to it. The extra parenthesis are a workaround (for what, exactly, i
      > don't remember).
      >
      >
      > > The
      > > aforementioned external json array has no name. It just starts with a
      > > paranthesis [
      > >
      >
      > After the above line runs, it has the name 'p' (the name you gave it).
      >
      > --
      > ----- stephan beal
      > http://wanderinghorse.net/home/stephan/
      >
      >
      > [Non-text portions of this message have been removed]
      >

      Thank you for your kind answer, but it does not address my problem: How to get the external json array into a text string (that can then be parsed).
    • Stephan Beal
      On Thu, May 19, 2011 at 4:41 PM, jonerlandmadsen
      Message 2 of 8 , May 19 8:12 AM
      • 0 Attachment
        On Thu, May 19, 2011 at 4:41 PM, jonerlandmadsen <
        jon.erland.madsen@...> wrote:

        > Thank you for your kind answer, but it does not address my problem: How to
        > get the external json array into a text string (that can then be parsed).
        >

        You already have it: the "contact" string is the array, as a JSON-formatted
        string.


        --
        ----- stephan beal
        http://wanderinghorse.net/home/stephan/


        [Non-text portions of this message have been removed]
      • Connie Clark
        Here are several patterns I often use which may help you *If the list data is available at the time your server is returning the rest of the page content *
        Message 3 of 8 , May 19 9:00 AM
        • 0 Attachment
          Here are several patterns I often use which may help you

          *If the list data is available at the time your server is
          returning the rest of the page content
          *
          Pattern.1:

          Have the server emit a script tag that pulls in
          your javascript code for doing something with the list,
          say a function called handleMyList.

          Have the server then emit (within script tags)
          handleMyList( [ item1, item2, item2 ] );


          Pattern.2:

          Have the server emit (within script tags)
          var listArr = [ item1, item2, item3 ];

          Then have the server emit a script tag that
          pulls in your javascript code for doing something
          with listArr.



          *If you need to request the list after the user has
          done other selections:*

          This is quite similar to Pattern.1, but you
          would use an AJAX style request to get the
          content back.

          *OR, to do it "by hand"*

          On your HTML page
          define an iframe IFR
          define a form F
          with action: serverUrl
          target: IFR
          and hidden fields for any parameters
          you need to pass

          When the user makes their selection
          F.parm1.value = "..."
          F.submit();

          Have the server return (within script tags)
          parent.top.handleMyList( [ item1, item2, item3 ]);

          Connie


          On Thu, May 19, 2011 at 6:47 AM, jonerlandmadsen <
          jon.erland.madsen@...> wrote:

          >
          > Hi!
          >
          > I am trying to get an answer to what must be JSON-question number two, but
          > apparently, the answer to my question is too obvious to be included in the
          > Q&As I've found.
          >
          > I will use an external JSON-array in my javascript. It originates from our
          > own server, so there are no security concerns.
          >
          > The address is be for instance:
          >
          > http://www.mysite.no/services/product/list/
          >
          > According to Wikipedia, I can make an object out of it, using "eval". The
          > example given is:
          >
          >
          > var p = eval("(" + contact + ")");
          >
          >
          > ..where 'contact' is the json array contained in a text object.
          >
          > But there must be a step between, that is not explained. The
          > aforementioned external json array has no name. It just starts with a
          > paranthesis [
          >
          > I would thus have to convert it into a string first. How do I do that
          > (javascript does not allow you to read external files into a string, as for
          > instance php does).
          >
          > Can I convert a json array directly using the url as an argument in eval()
          > (probably not)?
          >
          > I have programmed for 20 years (!) but never used JSON. I feel like a
          > complete fool. I've searched the internet two hours for the answer to
          > this. Even googling json+dummies or json+newbie didn't help. They all write
          > about different parsers.
          >
          > If you could help me, I would be very grateful.
          >
          >
          >
          >
          >
          >
          >
          >
          >
          > ------------------------------------
          >
          > Yahoo! Groups Links
          >
          >
          >
          >


          [Non-text portions of this message have been removed]
        • jon.erland.madsen@jiffymade.no
          Thank you so very much! I got help and will use the following code (my script is client side): ********************************************** function
          Message 4 of 8 , May 19 9:57 AM
          • 0 Attachment
            Thank you so very much!

            I got help and will use the following code (my script is client side):

            **********************************************

            function createXMLHttpRequest()
            {
            try { return new XMLHttpRequest(); } catch(e) {}
            try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
            return null;
            }

            var url = "http://www.finansportalen.no/services/sph/produkt/list/";
            var xhReq = createXMLHttpRequest();
            xhReq.open("GET", url, false);
            xhReq.send(null);

            var sphdata = eval(xhReq.responseText);

            **********************************************

            (I would have prefered, however, to have the object declared by the server
            and posted as a .js file i could simply include, thus enabling me to
            access it directly. But the above will do.)

            Thanks again!





            Here are several patterns I often use which may help you

            *If the list data is available at the time your server is
            returning the rest of the page content
            *
            Pattern.1:

            Have the server emit a script tag that pulls in
            your javascript code for doing something with the list,
            say a function called handleMyList.

            Have the server then emit (within script tags)
            handleMyList( [ item1, item2, item2 ] );


            Pattern.2:

            Have the server emit (within script tags)
            var listArr = [ item1, item2, item3 ];

            Then have the server emit a script tag that
            pulls in your javascript code for doing something
            with listArr.



            *If you need to request the list after the user has
            done other selections:*

            This is quite similar to Pattern.1, but you
            would use an AJAX style request to get the
            content back.

            *OR, to do it "by hand"*

            On your HTML page
            define an iframe IFR
            define a form F
            with action: serverUrl
            target: IFR
            and hidden fields for any parameters
            you need to pass

            When the user makes their selection
            F.parm1.value = "..."
            F.submit();

            Have the server return (within script tags)
            parent.top.handleMyList( [ item1, item2, item3 ]);

            Connie


            On Thu, May 19, 2011 at 6:47 AM, jonerlandmadsen <
            jon.erland.madsen@...> wrote:

            >
            > Hi!
            >
            > I am trying to get an answer to what must be JSON-question number two,
            > but
            > apparently, the answer to my question is too obvious to be included in
            > the
            > Q&As I've found.
            >
            > I will use an external JSON-array in my javascript. It originates from
            > our
            > own server, so there are no security concerns.
            >
            > The address is be for instance:
            >
            > http://www.mysite.no/services/product/list/
            >
            > According to Wikipedia, I can make an object out of it, using "eval".
            > The
            > example given is:
            >
            >
            > var p = eval("(" + contact + ")");
            >
            >
            > ..where 'contact' is the json array contained in a text object.
            >
            > But there must be a step between, that is not explained. The
            > aforementioned external json array has no name. It just starts with a
            > paranthesis [
            >
            > I would thus have to convert it into a string first. How do I do that
            > (javascript does not allow you to read external files into a string, as
            > for
            > instance php does).
            >
            > Can I convert a json array directly using the url as an argument in
            > eval()
            > (probably not)?
            >
            > I have programmed for 20 years (!) but never used JSON. I feel like a
            > complete fool. I've searched the internet two hours for the answer to
            > this. Even googling json+dummies or json+newbie didn't help. They all
            > write
            > about different parsers.
            >
            > If you could help me, I would be very grateful.
            >
            >
            >
            >
            >
            >
            >
            >
            >
            > ------------------------------------
            >
            > Yahoo! Groups Links
            >
            >
            >
            >


            [Non-text portions of this message have been removed]
          • kylealanhale
            I want to clarify a couple of things that no one else seems to have addressed: * There is no such thing as an external JSON-array ; JSON is just a data
            Message 5 of 8 , May 20 8:07 AM
            • 0 Attachment
              I want to clarify a couple of things that no one else seems to have
              addressed:
              * There is no such thing as "an external JSON-array"; JSON is just a
              data transfer format, or, more correctly, a data transfer notation
              (JavaScript Object Notation). What you described in your last post, of
              defining the array in a javascript file that you include in your page,
              is not JSON at all, just JavaScript.
              * You should never transfer JSON-encoded data as an array; this opens a
              security vulnerability (see
              http://directwebremoting.org/blog/joe/2007/03/05/json_is_not_as_safe_as_\
              people_think_it_is.html for a good summary of the issue); instead,
              always enclose it in an object. So instead of your data looking like
              [{"key1": "value1"}, {"key2": "value2"}...], make it look like {"list":
              [{"key1": "value1"}, {"key2": "value2"}...]}.
              * You should also never use eval(), for reasons that have been explained
              extensively elsewhere, including several times by the brainfather of
              JSON himself, and in the very Wikipedia article you quoted from
              originally <http://en.wikipedia.org/wiki/JSON#Security_issues>. Instead
              use JSON.parse, which is standard in all major browsers now.
              * If you want to get a JavaScript object from a JSON-encoded source
              while working from the browser, you will invariably use XHR (AJAX, if
              you must). The method you posted is exactly how you would do that, with
              the exception that you'd probably want to do it asynchronously and
              handle the data in a callback, rather than procedurally. Also, if you
              use a JavaScript framework, it will generally make your life much easier
              and your code much cleaner. A jQuery example:
              $.getJSON('http://www.mysite.no/services/product/list/', function(data)
              { $.each(data.list, function (index, listItem) { // Do something
              which each item in the list });});
              Note that this function takes care of (safely) decoding your
              JSON-encoded data so that you can handle it as a native JavaScript
              object. See http://api.jquery.com/jQuery.getJSON/ and
              http://api.jquery.com/jQuery.ajax/ for more details.
              * While there are exceptions to all of these, and things I have left
              out, this is a good starting place for best-practices, I think.


              --- In json@yahoogroups.com, jon.erland.madsen@... wrote:
              >
              > Thank you so very much!
              >
              > I got help and will use the following code (my script is client side):
              >
              > **********************************************
              >
              > function createXMLHttpRequest()
              > {
              > try { return new XMLHttpRequest(); } catch(e) {}
              > try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
              > return null;
              > }
              >
              > var url = "http://www.finansportalen.no/services/sph/produkt/list/";
              > var xhReq = createXMLHttpRequest();
              > xhReq.open("GET", url, false);
              > xhReq.send(null);
              >
              > var sphdata = eval(xhReq.responseText);
              >
              > **********************************************
              >
              > (I would have prefered, however, to have the object declared by the
              server
              > and posted as a .js file i could simply include, thus enabling me to
              > access it directly. But the above will do.)
              >
              > Thanks again!
              >



              [Non-text portions of this message have been removed]
            Your message has been successfully submitted and would be delivered to recipients shortly.