Henson
Henson

Reputation: 5923

Javascript Data Format, Is This JSON?

Some jquery plugins I use require a data of this format as its input type

[
{image : '1.jpg', title : 'Title', url : '', age : 24, location : 'US', name : 'John', description : 'Long Text'},  
{image : '1.jpg', title : 'Title', url : '', age : 24, location : 'US', name : 'John', description : 'Long Text'}, 
{image : '1.jpg', title : 'Title', url : '', age : 24, location : 'US', name : 'John', description : 'Long Text'}
]

My question is, what is this kind of data and how to create it? Is this still JSON? because when I try to pass json_encoded array with PHP and get it with jquery, I get this format :

[
{'image' : '1.jpg', 'title' : 'Title', 'url' : '', 'age' : 24, 'location' : 'US', 'name' : 'John', 'description' : 'Long Text'},  
{'image' : '1.jpg', 'title' : 'Title', 'url' : '', 'age' : 24, 'location' : 'US', 'name' : 'John', 'description' : 'Long Text'}, 
{'image' : '1.jpg', 'title' : 'Title', 'url' : '', 'age' : 24, 'location' : 'US', 'name' : 'John', 'description' : 'Long Text'}
]

Notice the quotes that wrap the variable name. That makes the code not working.

Upvotes: 2

Views: 339

Answers (3)

timothyclifford
timothyclifford

Reputation: 6969

I'm going to put this is another answer so it's easier to read...

In this instance you don't want to generate JSON, the plugin requires a literal array as Matt Ball said.

Basic code format will be something similar to this (will be semi .NET sorry...):

public Array GenerateData()
{
// Generate dynamic data here and return as a list / array
}

// Get the data
Array dynamicData = GenerateData();

// Create the template
string template = "[";

// For each item in list, add to the template
foreach (item in dynamicData)
{
    template += "{ image : '" + item[0] + "', title : '" + item[1] + "', url : '" + item[2] + "', age : " + item[3] + ", location : '" + item[4] + "', name : '" + item[5] + "', description : '" + item[6] + "' },"
}

// Add closing brack - may need to trim off last comma also?
string template = "]";

Then embed the template string in your plug intitialisation.

Upvotes: 0

timothyclifford
timothyclifford

Reputation: 6969

Doesn't appear that the plugins are requiring JSON - looks more like a dictionary of some kind.

Valid JSON looks something similar to: {"name":"John", "age":50}

You will probably need to build a custom helper method to format the data correctly for your plugins, can give you a hand if you provide a bit more of your source code :)

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 360046

The first is a JavaScript array literal which contains object literals. It is not JSON.

The second is also an array literal that contains objects. It's closer to JSON, but is not 100% valid, since JSON requires that all strings are double-quoted. For example, this is valid JSON:

[{"image": "1.jpg", "title": "Title" }]

If you're unsure if you're looking at valid JSON, you can always run it through JSONLint and see for yourself.

Upvotes: 5

Related Questions