what is JSON (short explanation)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    what is JSON (short explanation)

    Originally posted by gits
    hi ...

    while the standard HTTP request makes a 'synchronous' call and must wait for the response and makes a page-reload (you always get a new html-page to display) a XMLHttpRequest may be used sync (not typical) and async (the better way) without a page-reload. you may ask for the response with javascript and the response is usually xml- or json-data that you may process with js and update parts of your page through the use of dom-methods that manipulate your document ... so you don't need an entire page-reload because all of that is running in the 'background' ...

    kind regards

    Hi,
    Thank u moderator...
    will u please explain me the clear picture of json-Data..
    wat does it mean??
    and some sites to get basic knowledge in that...

    -Hamsa
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    no problem :) ... have a look at the following example that shows you a typical js-object:

    [CODE=javascript]// we create a complex array that contains 3 js
    // objects
    var js_obj = [
    {val1: 'value1', val2: 'value2'},
    {val1: 'value1', val2: 'value2'},
    {val1: 'value1', val2: 'value2'}
    ];[/CODE]

    json = javascript object notation and now we get from a, lets say php-json-encoder the following string (as you see it is simply a string that is notated like an native js-obj and only needs to be evaled):

    Code:
    "[
        {val1: 'value1', val2: 'value2'},
        {val1: 'value1', val2: 'value2'},
        {val1: 'value1', val2: 'value2'}
    ]"
    with that as a responseText you simply may do:

    [CODE=javascript]var data = eval(responseTe xt);[/CODE]

    and you get our native js-object in var data ... ok?

    kind regards

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      hi ... i splitted your JSON-question from the HttpRequest-Thread because i think it may be of interest for more users and so it would be better to find :)

      kind regards

      Comment

      • gaya3
        New Member
        • Aug 2007
        • 184

        #4
        Originally posted by gits
        hi ...

        no problem :) ... have a look at the following example that shows you a typical js-object:

        [CODE=javascript]// we create a complex array that contains 3 js
        // objects
        var js_obj = [
        {val1: 'value1', val2: 'value2'},
        {val1: 'value1', val2: 'value2'},
        {val1: 'value1', val2: 'value2'}
        ];[/CODE]

        json = javascript object notation and now we get from a, lets say php-json-encoder the following string (as you see it is simply a string that is notated like an native js-obj and only needs to be evaled):

        Code:
        "[
            {val1: 'value1', val2: 'value2'},
            {val1: 'value1', val2: 'value2'},
            {val1: 'value1', val2: 'value2'}
        ]"
        with that as a responseText you simply may do:

        [CODE=javascript]var data = eval(responseTe xt);[/CODE]

        and you get our native js-object in var data ... ok?

        kind regards

        Hi,
        Thank u...I m very new to this...
        still it looks greek n latin for me...
        ll u please say me still in depth...
        will u pl..

        -Hamsa

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          Originally posted by gaya3
          Hi,
          Thank u...I m very new to this...
          still it looks greek n latin for me...
          ll u please say me still in depth...
          will u pl..

          -Hamsa
          what exactly do you want to know? in the above post you first see a javascript array that has javascript-objects as elements. typical structure of a table ... lets say output of a sql-query. that is what we can handle with javascript ... a javascript-object (i put that to illustrate what we need in js this is what we handle at the client later on -> now we have to get it in any way from the database to the client). but ... the query is done at the server and now we must send its result to the client with a XMLHttpRequest. so the serverside has to encode the result as XML or JSON or whatever ... and when we use JSON-encoding you will send from the server what we see in the second part of the above post. your clientside awaits the responseText and is getting the string i showed you ... we take it, eval it and voila ... we have the js-object we now can handle with javacsript ...

          hope this makes it more clear ;)

          kind regards

          Comment

          • ManWithNoName
            New Member
            • Aug 2007
            • 88

            #6
            Hi, I'm new at JSON too.

            I think (I I'm neither a programmer nor a web-dev) you can use JSON strings to send objects and functions (?) back and forth client-side 'n server side with ease. I haven't looked into it so much (not at all in fact ;)) because my current project does not requires it.

            While I haven't got around to try it, I am though collecting good tutorials when I see them.

            Here are two that I like:





            Here is the official site: http://www.json.org/

            Comment

            • AMT India
              New Member
              • Feb 2007
              • 64

              #7
              Originally posted by gaya3
              Hi,
              Thank u moderator...
              will u please explain me the clear picture of json-Data..
              wat does it mean??
              and some sites to get basic knowledge in that...

              -Hamsa

              Read this article...very helpfull

              http://www.quirksmode. org/blog/archives/2005/12/the_ajax_respon .html

              Comment

              • ManWithNoName
                New Member
                • Aug 2007
                • 88

                #8
                Originally posted by gits

                [CODE=javascript]// we create a complex array that contains 3 js
                // objects
                var js_obj = [
                {val1: 'value1', val2: 'value2'},
                {val1: 'value1', val2: 'value2'},
                {val1: 'value1', val2: 'value2'}
                ];[/CODE]
                I'd like to see if I have understood this correctly:

                "Without JSON" those variables must have been sent as string, like:

                [CODE=javascript]
                var js_obj = "$a='value1 '; $b='value2';]";[/CODE] // etc.. (or simple stored in a array like structure)

                And then in PHP, split, exploded, implode, foreach, destroy, overkill... (or perhaps evaluated in PHP? I would guess that this would create a security issue if the data is user defined, no?)

                Originally posted by gits
                json = javascript object notation and now we get from a, lets say php-json-encoder the following string (as you see it is simply a string that is notated like an native js-obj and only needs to be evaled):

                Code:
                "[
                    {val1: 'value1', val2: 'value2'},
                    {val1: 'value1', val2: 'value2'},
                    {val1: 'value1', val2: 'value2'}
                ]"
                with that as a responseText you simply may do:

                [CODE=javascript]var data = eval(responseTe xt);[/CODE]

                and you get our native js-object in var data ... ok?
                Because of the fact that eval function evaluates anything, anything sent from the server side could be sent simple as var, array and/or functions, e.g. in PHP

                $sendBack = "var HelloCode = 'Hello World'; function alarm(str) {alert(str)};

                And then in JavaScript one could just run alarm(HelloCode );

                So in conclusion, JSON is a method for sending var, objects and functions to the server side? Have I got it right?

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  nope ...

                  i spoke about exactly the other direction ...

                  1. our client needs simply js data-structures like i showed with the var js_obj (to illustrate how such an obj looks like)

                  2. we make an ajax call to the server (GET / POST with required params - may be json too but needn't - in case they are we have to encode them at the client to be send correctly as a string and we mustn't eval there but decode/parse -> security reason and operations overhead ... i wouldn't recommend that) ...

                  3. the service makes a db-call, gets the result, fetches the lines and creates a php-obj that we now json-ENCODE and send back to the client

                  4. the client evals the json-response and we have a js-obj like in step 1.

                  kind regards

                  Comment

                  • ManWithNoName
                    New Member
                    • Aug 2007
                    • 88

                    #10
                    Originally posted by gits
                    nope ...

                    i spoke about exactly the other direction ...
                    ... I see that I still have some homework to do then. Well, then, maybe my example could work as a deterrent for others.

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      ... the conclusion could be ... that json is a better method of sending data to the client then XML would be ... because you will process it with js and it is overhead when sending/parsing XML with js ... since you simply may eval a string to have it js-ready with json ...

                      kind regards

                      Comment

                      Working...