0

I recently herd the buzz about JSON and stuff was also saw the coding style , but still i am not able to appreciate it .
Well i am working in python web framework (not Django) and also using templating language.

when i wrote a program to just to add two numbers , i found it a lot cumbersome than PHP.
Well i wouldnt get into JSON language , provided its worth it . i would be glad of u could throw some light on JSON .

3
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by samaru
0

Hi, IMHO, JSON is great, it has the same good benefits of structuring data as in XML but with the advantage that you can directoly interact in the front end using JS (that's what the two first letters of JSON means :-) ).

I use it everyday to store information in databases, handle arrays from a web service, etc.

I think that is very dynamic, and for me that is good.

(and you don't need a parser to read it clearly.)

just my two cents.

0

but how do u interact with databse using JSON ?
I thought people use php for that . can u give a small example of interaction with DB.

0

JSON doesn't interact directly with your database. Think about JSON as a data type, I usually use a BLOB to store big objects but even a simple LONGTEXT (or the one your DB supports) can do the job.

You must use your usual mechanism to connect and query your DB (Rubym python, php, java, asp, whatever.)

0

well honestly , i am kinda baffled . Well if its more on client side and still we gotto use the server side programming , then in wat way is it better than the existing javascript . Well may be i am going the wrong way , but i would be grateful if you could give me (or refer) some kinda example like form or soo . i have googled examples on json and i see , it more as a subsitute for javascript (conventional one ) rather than the xml thingy ur talking about.

0

Sorry I couldnt explain it better. I'm not trying to confuse you :-).

1. JSON is not a replacement for Javascript. It's javascript.

2. This is an example:

var person = [{name:'alex', country:'chile'}, {name:'rahul', country:'india'}];
	
	alert(person[0].name); //it will show 'alex'
	
	alert(person[1].country) // it will show 'india'

3. now you can see that the string :

[{name:'alex', country:'chile'}, {name:'rahul', country:'india'}]

Can be treated as plain text (you can use some available tools like jQuery to parse from and to JSON). You can store that piece of text in a DB or even in a cookie and then use it normally calling your backend.

0

now supposedly , i have the name and country list stored in the DB , then in that case can i still use JSON ?
for example i extract the info using php and then communicate that to JSON .. something like that .
or am i still getting it wrong ?

0

No, you are right.

If name and country are stored in a db, you can get that data using your server side language (python, php, java) and format the json string to output to the client. Then, in the client side you parse that string into a JSON object you can manipulate using Javascript in the way I show you before.

Example using PHP:

<?php

//connect to db
$out = "[";
//get your rows

$out .= "{name:'". $row['name']."', country:'".row['country']."'},";

//end processing your rows
$out .= "]";

echo $out;

?>

JSON has a structure (like arrays) and you must separate your entries usin commas (,). In the example above it ends with an extra ',', so you must take care of it.

0

It really depends what you mean "efficient."

XML has been around for a hell of a lot longer than JSON and its syntax has grown. The primary goal was two-fold when XML came out:

1. Way to standardize the description of data.
2. Be human readable enough so that it could be modified via a text editor.

Since it came out, it's syntax and feature set has grown. You can have attributes, namespaces, XML Schemas that define an XML document - there's standard parsing engines: XQuery, Xpath, etc. Some schemas have become mainstream, like RSS, Atom, SOAP, etc.

The drawback is of course, the more complex your data, the more text you have to use to describe it (you have to use more tags).

JSON was a way to send lighten the load. You can either do this (crude example):

<store>
<customer>
   <name>Dan</name>
   <alias>samaru</name>
</customer>
<customer>
   <name>Parker</name>
   <alias>Spider-Man</name>
</customer>
</store>

Or do:

store = {"customer1":{"name":"dan","alias":"samaru"},"customer2":{"name":"Parker", "alias":"Spider-Man"}};

Note that the right of the equal sign is JSON. Since JSON came from JavaScript, you can literally use that JSON as a Javascript object. In this case, it's being set to an object named store. That's the benefit of JSON. However, it's more cryptic to read to the naked eye then XML. Also, there aren't really any standards like there are in XML.

If you're concerned about the compatibility across systems (exchanging data and whatnot) and dealing with legacy system, then go with XML. If you're not planning on exchanging the data across other systems and just loading data to an XMLHttpRequest object for an AJAX request, then go with JSON.

Edited by samaru: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.