JSON Basics: What You Need to Know

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

24-Mar-11 00:00
This is a forum topic for discussing the article "JSON Basics: What You Need to Know":

http://www.elated.com/articles/json-basics/

What is JSON, and what can it do? In this tutorial you learn how to use JSON to easily move data around the web. You also look at how to work with JSON using both JavaScript and PHP.
01-Apr-11 17:53
I have been reading ans studying your article on Json and it is really good but I can't seem to parse and display the individual elements of the following. I am pasting the json and the code I'm trying to use. I modified it from your example but of course I'm doing something wrong.

I would rather not use the 'true' to create the array but would like to know how to parse this json both ways.

Thank You for your help,

Ken




$jsonString = '
{
"Report":{"@attributes":{
"Name":"rpt_Exception"
},
"table1":{
"@attributes":{
"textbox1":"Car dealership",
"textbox15":"R&R Data: 3\/31\/2011 8:17:42 AM"
},
"grp_Account_Collection":
{
"grp_Account":[
{
"textbox17":"TOTAL for Acct 205 => 10 Days",
"textbox21":"819,593"
},
"Grp_Carline_Collection":{"Grp_Carline":
{
"Detail_Collection":
{
"Detail":[
{"@attributes":
{
"Account":"205",
"Description":"Contracts In Transit",
"ControlNumber":"8888",
"AgeInDays":"95",
"Balance":"2,003.32",
"CustomerNo":"99999",
"CustomerName":"Davis",
"BankNameOrCarLine":"Auto Finance"
}
},
{"@attributes":
{
"Account":"205",
"Description":"Contracts In Transit",
"ControlNumber":"7777",
"AgeInDays":"82",
"Balance":"(1,123.34)",
"CustomerNo":"66666",
"CustomerName":"Holmes",
"BankNameOrCarLine":"C A C"
}
},
],
}
}
],
}
}
}
}
';

$cart = json_decode( $jsonString,true );
print_r($cart);
04-Apr-11 02:38
@kvandiver: This tool is very useful

http://www.jsonlint.com/

As you can see, the problem is on line 17 of your JSON. You've created an array called "grp_Account", so every item within that array needs to be a single value (such as another array or an object). However, on line 17 you have not a single value, but a name/value pair ("Grp_Carline_Collection": {...}).

Hope that helps!
Matt

--
Matt Doyle, Elated
28-Apr-11 16:07
Aw, this article was going so well until I got to the XML section. No one in their right mind would write XML like that! In XML, the element names themselves are the keys.

Something along these lines would be the correct way:


<cart>
<orderId>12345</orderId>
<shopperName>John Smith</shopperName>
<shopperEmail>johnsmith@example.com</shopperEmail>
<orderCompleted>true</orderCompleted>

<contents>
<product id="34" quantity="1">SuperWidget</product>
<product id="56" quantity="3">WonderWidget</product>
</contents>
</cart>


This turns out to be 286 characters without spaces, as opposed to 236 for JSON. Not a huge difference. (Personally I still prefer JSON since it's dead simple to parse and work with in Javascript/PHP.)
28-Apr-11 19:20
@sockpuppet: Agreed. I was just trying to show the extreme case with XML. There are many ways to write XML, and there are also some cases where avoiding attributes makes sense. But in most situations I would definitely use your more compact version.

Here's a good discussion:

http://nedbatchelder.com/blog/200412/elements_vs_attributes.html

The point still stands though: JSON is generally more compact than XML.

Edit: I've added a note to the article to clarify. Thanks!

[Edited by matt on 28-Apr-11 19:36]

--
Matt Doyle, Elated
21-Apr-12 09:45
Matt ,

I love your tutorials man!

--
Tony
04-May-12 05:27
@tonyb67: Thanks

--
Matt Doyle, Elated
22-Mar-13 14:49
I was wondering if you can help with this. I am sending a jquery:


var jsonData = $.ajax({
type: "GET",
url: "http://127.0.0.1:8080/getData.php",
dataType:"json",
async: false,
error: function(xhr, s, t) {
alert("xhr: " + xhr.status + " " + xhr.statusText + " " + s + " " + t);
}
}).responseText;



but get an error. which is:
...NS_ERROR_FAILURE http://code.jquery.com/jquery-1.9.1.js
...send line 8526 data: no
It is failing in sending the request, looks like. Any clue
what could be wrong or how to fix the problem?
getdata.php simply reads a file in json syntax and sends
it back:

<?php

$string = file_get_contents("enroll-1.json");
echo $string;

?>
If its' failing in the send then it can't be json syntax problem. It should at least send the request.
I ahve spent over 8 hours trygint to figure this out, any help
is greatly appreciated.

Thanks.

--
uufilter

 
New posts
Old posts

Follow Elated