/    Sign up×
Community /Pin to ProfileBookmark

Problem with json and xml

I hava an xml file with the following structure:

<?xml version=”1.0″ encoding=”iso-8859-1″ standalone=”yes”?>

<publications>

<book>
<title>Test Title 1</title>
<author>Test author 1</author>
<year></year>
</book>

<book>
<title>Test Title 2</title>
<author>Test author 2</author>
<year></year>
</book>

</publications>

This xml is loaded into a php object with SimpleXml, converted in a json string and sent to output.

On the client-side, the string is received by a Javascript and converted back to an object using the Prototype function evalJSON. The received string, before the decoding, looks as follows:

{“book”:[{“title”:”Test Title 1″,”author”:”Test author 1″,”year”:{}},{“title”:”Test Title 2″,”author”:”Test author 2″,”year”:{}}]}

All the properties of the re-created object are ok and the property “length” says correctly 2.

Now the problem:

If one node in the original xml is deleted, so that the xml contains now only one node, the whole thing doesn’t work. More exactly, with only one node the received json string loses the square brackets and looks as follows:

{“book”:{“title”:”Test Title 1″,”author”:”Test author 1″,”year”:{}}}

With this structure, after the decoding with evalJSON the object is not correctly recreated and its property length is set to “undefined”.

As a test, I manually re-inserted the square brackets in the correct position of the json string, and everything works again as espected.

Is anyone experiencing the same issue? This creates a lot of problems with the correct view of any photo-gallery, product-catalogue and so on which uses xml as database.

Thanks

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jun 29.2008 — It's a simple enough fix:<i>
</i>if (typeof jsonobject.book != "array") {
jsonobject.book = [jsonobject.book];
}
Copy linkTweet thisAlerts:
@StenanauthorJun 29.2008 — Thanks!

BUT: unfortunately, jsonobject.book is always evaluated as an object, not as an array. So, the condition is satisfied in any case. Of course, I can work around the problem with some other test as:

if (typeof (jsonobject.book.length) == "undefined") {

....

}

But, maybe you have any "smarter" solution?

Stefano
Copy linkTweet thisAlerts:
@Declan1991Jun 29.2008 — Well with the examples you have given, you can test it yourself.<i>
</i>var jsonobject = {"book":{"title":"Test Title 1","author":"Test author 1","year":{}}};
var jsonobject = {"book":[{"title":"Test Title 1","author":"Test author 1","year":{}},{"title":"Test Title 2","author":"Test author 2","year":{}}]};
alert(typeof jsonobject.book+"n"+typeof jsonobject1.book);

[] is JSON for array,

{} is JSON for object.
Copy linkTweet thisAlerts:
@StenanauthorJun 30.2008 — Ok, thank you very much.
Copy linkTweet thisAlerts:
@seatoskyhkJun 30.2008 — I had that problem before.

What i did is to check whether it is array or not (as answered above).

so
[CODE]
if (abc.data.isArray())
{
for (var i=0; i<abc.data.length; i++)
{
result += abc.data[i];
}
} else
{
result = abc.data;
}
[/CODE]
Copy linkTweet thisAlerts:
@StenanauthorJul 01.2008 — Thank you very much. I wonder if it would be worth to put the problem to the attention of the creators of Prototype and other libraries. I can imagine that some web developer could realize the problem only the first time that, say, the user attempts to delete the last two pictures in a gallery...
×

Success!

Help @Stenan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...