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

Re: Hello all I am new to json so I have a few questions

Expand Messages
  • marcrbraincast
    ... The page reads: Dependencies JSONError.js I guess you need that file included too...
    Message 1 of 15 , Jun 27, 2007
    • 0 Attachment
      --- In json@yahoogroups.com, "timgerr" <tim.gallagher@...> wrote:
      >
      > I am trying to work some examples from
      > http://www.devpro.it/JSON/files/JSON-js.html and I keep getting an
      > error "JSON is not defined." Do I have to declaire if someware? Here
      > is the code that I am trying.
      >
      >
      > <html>
      > <head>
      > <title>Learn Json v.01</title>
      > <script type="text/javascript" src="json.js"></script>
      > <script type="text/javascript"
      > src="prototype.js"></script>
      > <script type="text/javascript">
      > var arr = JSON.decode('[1,2,3]');
      > alert(arr); // 1,2,3
      > </script>
      >
      >
      > </head>
      > </html>
      >
      > I have no ide what is wrong, thanks for your time.
      >
      > -T
      >

      The page reads:

      Dependencies
      JSONError.js

      I guess you need that file included too...
    • timgerr
      ... an ... someware? Here ... src= json.js ... Thank you for the information, I guess that I need to read better. Here is the biggest question that
      Message 2 of 15 , Jun 27, 2007
      • 0 Attachment
        --- In json@yahoogroups.com, "marcrbraincast" <marcrbraincast@...>
        wrote:
        >
        > --- In json@yahoogroups.com, "timgerr" <tim.gallagher@> wrote:
        > >
        > > I am trying to work some examples from
        > > http://www.devpro.it/JSON/files/JSON-js.html and I keep getting
        an
        > > error "JSON is not defined." Do I have to declaire if
        someware? Here
        > > is the code that I am trying.
        > >
        > >
        > > <html>
        > > <head>
        > > <title>Learn Json v.01</title>
        > > <script type="text/javascript"
        src="json.js"></script>
        > > <script type="text/javascript"
        > > src="prototype.js"></script>
        > > <script type="text/javascript">
        > > var arr = JSON.decode('[1,2,3]');
        > > alert(arr); // 1,2,3
        > > </script>
        > >
        > >
        > > </head>
        > > </html>
        > >
        > > I have no ide what is wrong, thanks for your time.
        > >
        > > -T
        > >
        >
        > The page reads:
        >
        > Dependencies
        > JSONError.js
        >
        > I guess you need that file included too...
        >


        Thank you for the information, I guess that I need to read better.
        Here is the biggest question that I have, lets say I have a string

        var test = {"outPut":[{"name":"Michelle Gallagher"},{"name":"Bob
        Jones"},{"name":"tester"},{"name":"Administrator"}]};

        Can I decode this back into an object?

        Thanks again for the help,
        -T
      • marcrbraincast
        ... ... the var test is already an object. It s an object with one array which holds 4 objects. var str = { output :[ name : Michelle ]} ; would make a
        Message 3 of 15 , Jun 27, 2007
        • 0 Attachment
          --- In json@yahoogroups.com, "timgerr" <tim.gallagher@...> wrote:
          >
          <cut>
          > >
          > > The page reads:
          > >
          > > Dependencies
          > > JSONError.js
          > >
          > > I guess you need that file included too...
          > >
          >
          >
          > Thank you for the information, I guess that I need to read better.
          > Here is the biggest question that I have, lets say I have a string
          >
          > var test = {"outPut":[{"name":"Michelle Gallagher"},{"name":"Bob
          > Jones"},{"name":"tester"},{"name":"Administrator"}]};
          >
          > Can I decode this back into an object?
          >
          > Thanks again for the help,
          > -T
          >

          the var test is already an object.
          It's an object with one array which holds 4 objects.

          var str = "{'output':['name':'Michelle']}";
          would make a string...
        • marcrbraincast
          woops... ... var str = { output :[{ name : Michelle }]} ; would make a string...
          Message 4 of 15 , Jun 27, 2007
          • 0 Attachment
            woops...
            >
            var str = "{'output':[{'name':'Michelle'}]}";
            would make a string...
          • timgerr
            ... better. ... string ... I am new to some of this so I might not be clear, I am woking with ajax and I am getting a string returned from another webpage that
            Message 5 of 15 , Jun 27, 2007
            • 0 Attachment
              --- In json@yahoogroups.com, "marcrbraincast" <marcrbraincast@...>
              wrote:
              >
              > --- In json@yahoogroups.com, "timgerr" <tim.gallagher@> wrote:
              > >
              > <cut>
              > > >
              > > > The page reads:
              > > >
              > > > Dependencies
              > > > JSONError.js
              > > >
              > > > I guess you need that file included too...
              > > >
              > >
              > >
              > > Thank you for the information, I guess that I need to read
              better.
              > > Here is the biggest question that I have, lets say I have a
              string
              > >
              > > var test = {"outPut":[{"name":"Michelle Gallagher"},{"name":"Bob
              > > Jones"},{"name":"tester"},{"name":"Administrator"}]};
              > >
              > > Can I decode this back into an object?
              > >
              > > Thanks again for the help,
              > > -T
              > >
              >
              > the var test is already an object.
              > It's an object with one array which holds 4 objects.
              >
              > var str = "{'output':['name':'Michelle']}";
              > would make a string...
              >


              I am new to some of this so I might not be clear, I am woking with
              ajax and I am getting a string returned from another webpage that is
              JSON encoded. When I recieve it it is then consittered a string. I
              would like to take this returned string
              {"outPut":[{"name":"Michelle Gallagher"},{"name":"Bob Jones"},
              {"name":"tester"},{"name":"Administrator"}]}
              and put it back into a javascript object. Can I do that with decode?

              Thanks for the help,
              -T
            • Dan Joseph
              I am new to some of this so I might not be clear, I am woking with ... I think you might be looking for eval(). Here is what I do with my json requests: {
              Message 6 of 15 , Jun 27, 2007
              • 0 Attachment
                I am new to some of this so I might not be clear, I am woking with

                > ajax and I am getting a string returned from another webpage that is
                > JSON encoded. When I recieve it it is then consittered a string. I
                > would like to take this returned string
                > {"outPut":[{"name":"Michelle Gallagher"},{"name":"Bob Jones"},
                > {"name":"tester"},{"name":"Administrator"}]}
                > and put it back into a javascript object. Can I do that with decode?
                >
                > _._,___
                >


                ---------------------

                I think you might be looking for eval(). Here is what I do with my json
                requests:

                { "errors" :
                [
                { "code" : "OK" }
                ];
                "cells" :
                [';
                { "cell" : "none" }
                ] }

                var j = eval('('+ xmlhttp.responseText +')');

                And then they are accessible in the form of: j.errors[0].code so if I do an
                alert( j.errors[0].code ); I'd get OK (the value of code). I'm pretty new
                to this also, so you may want to take my brief example and search around
                yahoo and google for a similar more in depth tutorial.

                --
                -Dan Joseph

                "Build a man a fire, and he will be warm for the rest of the day.
                Light a man on fire, and will be warm for the rest of his life."


                [Non-text portions of this message have been removed]
              • marcrbraincast
                ... I do an ... pretty new ... Hi Guys, That would mean that you are letting javascript evaluating possible unsafe json... thats why you should either use
                Message 7 of 15 , Jun 27, 2007
                • 0 Attachment
                  --- In json@yahoogroups.com, "Dan Joseph" <dmjoseph@...> wrote:
                  >
                  > I am new to some of this so I might not be clear, I am woking with
                  >
                  > > ajax and I am getting a string returned from another webpage that is
                  > > JSON encoded. When I recieve it it is then consittered a string. I
                  > > would like to take this returned string
                  > > {"outPut":[{"name":"Michelle Gallagher"},{"name":"Bob Jones"},
                  > > {"name":"tester"},{"name":"Administrator"}]}
                  > > and put it back into a javascript object. Can I do that with decode?
                  > >
                  > > _._,___
                  > >
                  >
                  >
                  > ---------------------
                  >
                  > I think you might be looking for eval(). Here is what I do with my json
                  > requests:
                  >
                  > { "errors" :
                  > [
                  > { "code" : "OK" }
                  > ];
                  > "cells" :
                  > [';
                  > { "cell" : "none" }
                  > ] }
                  >
                  > var j = eval('('+ xmlhttp.responseText +')');
                  >
                  > And then they are accessible in the form of: j.errors[0].code so if
                  I do an
                  > alert( j.errors[0].code ); I'd get OK (the value of code). I'm
                  pretty new
                  > to this also, so you may want to take my brief example and search around
                  > yahoo and google for a similar more in depth tutorial.
                  >
                  > --
                  > -Dan Joseph
                  >
                  > "Build a man a fire, and he will be warm for the rest of the day.
                  > Light a man on fire, and will be warm for the rest of his life."
                  >
                  >
                  > [Non-text portions of this message have been removed]
                  >

                  Hi Guys,

                  That would mean that you are letting javascript evaluating possible
                  unsafe json... thats why you should either use json.js from Douglas
                  (http://www.json.org/json.js) or the JSON.js from devpro.it...

                  If the json object/string is safe because you are generating it or it
                  comes from a safe source... then by all means use eval()...

                  I am unfamiliar with the JSON.js library so I would say give it a go
                  with the JSONError.js in there as well... if it still gives problems
                  you might want to use http://www.jslint.com/lint.html to check the
                  json... otherwise, why don't you ask the guy from devpro.it?

                  And then there's always the possibility to just post as much code as
                  you are willing to share with the json so we can test it and see what
                  errors you are getting ;p
                • Mark Ireland
                  For a smaller jsObject can there be a json variable like this: { totalItems :40, itemsFound :2, UNIQUEIDS :[ js63013 , js63009 ],
                  Message 8 of 15 , Jun 27, 2007
                  • 0 Attachment
                    For a smaller jsObject can there be a json variable like this:

                    {"totalItems":40, "itemsFound":2, "UNIQUEIDS":["js63013","js63009"],
                    "columnnames":["Company,Title,Name,Phone,Email"], items: ["js63013":
                    {"A1 Services",
                    "CEO",
                    "Jane Jones",
                    "800-555-2121",
                    "janejones@..."},

                    "js63009":{"Acme",
                    "President",
                    "John Smith",
                    "800-555-1212",
                    "johnsmith@..."}
                    ]}


                    rather than this:

                    {SalesDatabase2:
                    {totalItems:40, itemsFound:2, items: [
                    {Company:"A1 Services",
                    Title:"CEO",
                    Name:"Jane Jones",
                    Phone:"800-555-2121",
                    Email:"janejones@..."},

                    {Company:"Acme",
                    Title:"President",
                    Name:"John Smith",
                    Phone:"800-555-1212",
                    Email:"johnsmith@..."}
                    ]}
                    }

                    _________________________________________________________________
                    Advertisement: ninemsn Travel - Hot deals, travel ideas & Lonely Planet
                    guides.
                    http://ninemsn.com.au/share/redir/adTrack.asp?mode=click&clientID=799&referral=hotmailtagline&URL=http://travel.ninemsn.com.au/compIntro.aspx?compId=2404
                  • marcrbraincast
                    ... UNIQUEIDS :[ js63013 , js63009 ], ...
                    Message 9 of 15 , Jun 27, 2007
                    • 0 Attachment
                      --- In json@yahoogroups.com, "Mark Ireland" <markincuba@...> wrote:
                      >
                      >
                      > For a smaller jsObject can there be a json variable like this:
                      >
                      > {"totalItems":40, "itemsFound":2,
                      "UNIQUEIDS":["js63013","js63009"],
                      > "columnnames":["Company,Title,Name,Phone,Email"], items: ["js63013":
                      > {"A1 Services",
                      > "CEO",
                      > "Jane Jones",
                      > "800-555-2121",
                      > "janejones@..."},
                      >
                      > "js63009":{"Acme",
                      > "President",
                      > "John Smith",
                      > "800-555-1212",
                      > "johnsmith@..."}
                      > ]}
                      >
                      >
                      > rather than this:
                      >
                      > {SalesDatabase2:
                      > {totalItems:40, itemsFound:2, items: [
                      > {Company:"A1 Services",
                      > Title:"CEO",
                      > Name:"Jane Jones",
                      > Phone:"800-555-2121",
                      > Email:"janejones@..."},
                      >
                      > {Company:"Acme",
                      > Title:"President",
                      > Name:"John Smith",
                      > Phone:"800-555-1212",
                      > Email:"johnsmith@..."}
                      > ]}
                      > }
                      >
                      > _________________________________________________________________
                      > Advertisement: ninemsn Travel - Hot deals, travel ideas & Lonely Planet
                      > guides.
                      >
                      http://ninemsn.com.au/share/redir/adTrack.asp?mode=click&clientID=799&referral=hotmailtagline&URL=http://travel.ninemsn.com.au/compIntro.aspx?compId=2404
                      >

                      Then it would probably become something like this:

                      {"totalItems":40,"itemsFound":2,"UNIQUEIDS":["js63013","js63009"],"columnnames":["Company","Title","Name","Phone","Email"],"items":{"js63013":["A1
                      Services","CEO","Jane Jones","800-555-2121","janejones@a1service
                      s.com"],"js63009":[" Acme","President","John
                      Smith","800-555-1212","johnsmith@acme. com"]}}

                      But although that probably results in a somewhat smaller json string I
                      don't really see a benefit, in fact, the object looks a lot more
                      difficult to get the value's form.
                    • marcrbraincast
                      ... Planet ...
                      Message 10 of 15 , Jun 27, 2007
                      • 0 Attachment
                        --- In json@yahoogroups.com, "marcrbraincast" <marcrbraincast@...> wrote:
                        >
                        > --- In json@yahoogroups.com, "Mark Ireland" <markincuba@> wrote:
                        > >
                        > >
                        > > For a smaller jsObject can there be a json variable like this:
                        > >
                        > > {"totalItems":40, "itemsFound":2,
                        > "UNIQUEIDS":["js63013","js63009"],
                        > > "columnnames":["Company,Title,Name,Phone,Email"], items: ["js63013":
                        > > {"A1 Services",
                        > > "CEO",
                        > > "Jane Jones",
                        > > "800-555-2121",
                        > > "janejones@"},
                        > >
                        > > "js63009":{"Acme",
                        > > "President",
                        > > "John Smith",
                        > > "800-555-1212",
                        > > "johnsmith@"}
                        > > ]}
                        > >
                        > >
                        > > rather than this:
                        > >
                        > > {SalesDatabase2:
                        > > {totalItems:40, itemsFound:2, items: [
                        > > {Company:"A1 Services",
                        > > Title:"CEO",
                        > > Name:"Jane Jones",
                        > > Phone:"800-555-2121",
                        > > Email:"janejones@"},
                        > >
                        > > {Company:"Acme",
                        > > Title:"President",
                        > > Name:"John Smith",
                        > > Phone:"800-555-1212",
                        > > Email:"johnsmith@"}
                        > > ]}
                        > > }
                        > >
                        > > _________________________________________________________________
                        > > Advertisement: ninemsn Travel - Hot deals, travel ideas & Lonely
                        Planet
                        > > guides.
                        > >
                        >
                        http://ninemsn.com.au/share/redir/adTrack.asp?mode=click&clientID=799&referral=hotmailtagline&URL=http://travel.ninemsn.com.au/compIntro.aspx?compId=2404
                        > >
                        >
                        > Then it would probably become something like this:
                        >
                        >
                        {"totalItems":40,"itemsFound":2,"UNIQUEIDS":["js63013","js63009"],"columnnames":["Company","Title","Name","Phone","Email"],"items":{"js63013":["A1
                        > Services","CEO","Jane Jones","800-555-2121","janejones@a1service
                        > s.com"],"js63009":[" Acme","President","John
                        > Smith","800-555-1212","johnsmith@acme. com"]}}
                        >
                        > But although that probably results in a somewhat smaller json string I
                        > don't really see a benefit, in fact, the object looks a lot more
                        > difficult to get the value's form.
                        >

                        grrr.... let's try that again... :/


                        {"totalItems":40,
                        "itemsFound":2,
                        "UNIQUEIDS":["js63013","js63009"],
                        "columnnames":["Company","Title","Name","Phone","Email"],
                        "items":{"js63013":
                        ["A1 Services","CEO","Jane Jones",
                        "800-555-2121","janejones@..."],
                        "js63009":
                        [" Acme","President","John Smith",
                        "800-555-1212","johnsmith@acme. com"]}}
                      • Don Owens
                        Do you mean the other way around? I.e., using unquoted keys? The JSON spec says that the keys must be quoted. The reason for this is that there is a bug in
                        Message 11 of 15 , Jun 28, 2007
                        • 0 Attachment
                          Do you mean the other way around? I.e., using unquoted keys? The
                          JSON spec says that the keys must be quoted. The reason for this is
                          that there is a bug in Javascript where it will treat a key as a
                          keyword if it is unquoted and matches a keyword. Javascript is not
                          smart enough to realize it should not be treating the key as a keyword.


                          --- In json@yahoogroups.com, "Mark Ireland" <markincuba@...> wrote:
                          >
                          >
                          > For a smaller jsObject can there be a json variable like this:
                          >
                          > {"totalItems":40, "itemsFound":2,
                          "UNIQUEIDS":["js63013","js63009"],
                          > "columnnames":["Company,Title,Name,Phone,Email"], items: ["js63013":
                          > {"A1 Services",
                          > "CEO",
                          > "Jane Jones",
                          > "800-555-2121",
                          > "janejones@..."},
                          >
                          > "js63009":{"Acme",
                          > "President",
                          > "John Smith",
                          > "800-555-1212",
                          > "johnsmith@..."}
                          > ]}
                          >
                          >
                          > rather than this:
                          >
                          > {SalesDatabase2:
                          > {totalItems:40, itemsFound:2, items: [
                          > {Company:"A1 Services",
                          > Title:"CEO",
                          > Name:"Jane Jones",
                          > Phone:"800-555-2121",
                          > Email:"janejones@..."},
                          >
                          > {Company:"Acme",
                          > Title:"President",
                          > Name:"John Smith",
                          > Phone:"800-555-1212",
                          > Email:"johnsmith@..."}
                          > ]}
                          > }
                          >
                          > _________________________________________________________________
                          > Advertisement: ninemsn Travel - Hot deals, travel ideas & Lonely Planet
                          > guides.
                          >
                          http://ninemsn.com.au/share/redir/adTrack.asp?mode=click&clientID=799&referral=hotmailtagline&URL=http://travel.ninemsn.com.au/compIntro.aspx?compId=2404
                          >
                        • Don Owens
                          Yes, via the parseJSON method called on a string (see http://www.json.org/json.js). Don t use eval() unless the JSON is from a trusted source, i.e., you know
                          Message 12 of 15 , Jun 28, 2007
                          • 0 Attachment
                            Yes, via the parseJSON method called on a string (see
                            http://www.json.org/json.js). Don't use eval() unless the JSON is
                            from a trusted source, i.e., you know there are no function calls in
                            there.


                            --- In json@yahoogroups.com, "timgerr" <tim.gallagher@...> wrote:
                            >
                            > --- In json@yahoogroups.com, "marcrbraincast" <marcrbraincast@>
                            > wrote:
                            > >
                            > > --- In json@yahoogroups.com, "timgerr" <tim.gallagher@> wrote:
                            > > >
                            > > <cut>
                            > > > >
                            > > > > The page reads:
                            > > > >
                            > > > > Dependencies
                            > > > > JSONError.js
                            > > > >
                            > > > > I guess you need that file included too...
                            > > > >
                            > > >
                            > > >
                            > > > Thank you for the information, I guess that I need to read
                            > better.
                            > > > Here is the biggest question that I have, lets say I have a
                            > string
                            > > >
                            > > > var test = {"outPut":[{"name":"Michelle Gallagher"},{"name":"Bob
                            > > > Jones"},{"name":"tester"},{"name":"Administrator"}]};
                            > > >
                            > > > Can I decode this back into an object?
                            > > >
                            > > > Thanks again for the help,
                            > > > -T
                            > > >
                            > >
                            > > the var test is already an object.
                            > > It's an object with one array which holds 4 objects.
                            > >
                            > > var str = "{'output':['name':'Michelle']}";
                            > > would make a string...
                            > >
                            >
                            >
                            > I am new to some of this so I might not be clear, I am woking with
                            > ajax and I am getting a string returned from another webpage that is
                            > JSON encoded. When I recieve it it is then consittered a string. I
                            > would like to take this returned string
                            > {"outPut":[{"name":"Michelle Gallagher"},{"name":"Bob Jones"},
                            > {"name":"tester"},{"name":"Administrator"}]}
                            > and put it back into a javascript object. Can I do that with decode?
                            >
                            > Thanks for the help,
                            > -T
                            >
                          • Mark Ireland
                            Somewhat smaller? I was thinking a large object would be shrunk by half because there would be no repetition of the columnnames everywhere. . ... And I thought
                            Message 13 of 15 , Jun 28, 2007
                            • 0 Attachment
                              Somewhat smaller? I was thinking a large object would be shrunk by half
                              because there would be no repetition of the columnnames everywhere. . ...

                              And I thought my idea would make getting a value as easy as

                              myJSON["js63009"].Company

                              I suppose the list of columnnames could be an array since people would
                              propbably make it into one so they can loop over it?

                              >Then it would probably become something like this:
                              >
                              >{"totalItems":40,"itemsFound":2,"UNIQUEIDS":["js63013","js63009"],"columnnames":["Company","Title","Name","Phone","Email"],"items":{"js63013":["A1
                              >Services","CEO","Jane Jones","800-555-2121","janejones@a1service
                              >s.com"],"js63009":[" Acme","President","John
                              >Smith","800-555-1212","johnsmith@acme. com"]}}
                              >
                              >But although that probably results in a somewhat smaller json string I
                              >don't really see a benefit, in fact, the object looks a lot more
                              >difficult to get the value's form.
                              >
                              >

                              _________________________________________________________________
                              Advertisement: ninemsn Travel - Hot deals, travel ideas & Lonely Planet
                              guides.
                              http://ninemsn.com.au/share/redir/adTrack.asp?mode=click&clientID=799&referral=hotmailtagline&URL=http://travel.ninemsn.com.au/compIntro.aspx?compId=2404
                            • marcrbraincast
                              Okay, fair enough... it will get smaller... You could indeed loose the list of columns and even the UNIQUEIDS and the totalItems... Not sure what the
                              Message 14 of 15 , Jun 29, 2007
                              • 0 Attachment
                                Okay, fair enough... it will get smaller...
                                You could indeed loose the list of columns and even the UNIQUEIDS and
                                the totalItems...

                                Not sure what the itemsFound does...


                                var j = {"itemsFound":2,
                                "items":[
                                {"js63013":["A1 Services","CEO","Jane
                                Jones","800-555-2121","janejones@..."]},
                                {"js63009":[" Acme","President","John
                                Smith","800-555-1212","johnsmith@acme. com"]}
                                ]
                                };
                                window.onload = function(){
                                var txt = "";
                                var col = j.items;
                                var totalitems = j.items.length;
                                for (var i=0;i<totalitems;i++){
                                for (j in col[i]){
                                txt += col[i][j] + " \n";
                                }

                                }
                                alert(totalitems + "\n" + txt);
                                }







                                --- In json@yahoogroups.com, "Mark Ireland" <markincuba@...> wrote:
                                >
                                >
                                > Somewhat smaller? I was thinking a large object would be shrunk by half
                                > because there would be no repetition of the columnnames everywhere.
                                . ...
                                >
                                > And I thought my idea would make getting a value as easy as
                                >
                                > myJSON["js63009"].Company
                                >
                                > I suppose the list of columnnames could be an array since people would
                                > propbably make it into one so they can loop over it?
                                >
                                > >Then it would probably become something like this:
                                > >
                                >
                                >{"totalItems":40,"itemsFound":2,"UNIQUEIDS":["js63013","js63009"],"columnnames":["Company","Title","Name","Phone","Email"],"items":{"js63013":["A1
                                > >Services","CEO","Jane Jones","800-555-2121","janejones@a1service
                                > >s.com"],"js63009":[" Acme","President","John
                                > >Smith","800-555-1212","johnsmith@acme. com"]}}
                                > >
                                > >But although that probably results in a somewhat smaller json string I
                                > >don't really see a benefit, in fact, the object looks a lot more
                                > >difficult to get the value's form.
                                > >
                                > >
                                >
                              Your message has been successfully submitted and would be delivered to recipients shortly.