开发者

Simple JSON problem

I never worked with JSON and I really need to do it now, I've tried bunch of examples from jQuery page but it does not seem to work for me.

I have *.php file that is generating string, as I understand this is how I pass JSON to javascript from PHP. The string that I want to generate is very simple:

foreach ($imageList as $img)
{
    echo "{ thumb: 'media/img/t/$img', image: 'media/img/m/$img'},";
}

So the result would be something like (formatted)

{
thumb: 'media/img/t/123.jpg',
image: 'media/img/m/123.jpg'
},
{
thumb: 'media/img/t/1234.jpg',
image: 'media/img/m/1234.jpg'
},

and so on...

Somehow I think this is not the correct way of doing things, excuse me of course, but as I told I never worked with JSON format and I barely know what it even stands for.

I want this data to be located in javascript array so I can use it in my gallery like here (3. Using JSON) http://galleria开发者_开发技巧.aino.se/docs/1.2/references/data/

Can someone help me with this one? I know that it is probably very easy problem, but for me as a total noob it is hard to understand. When I do know how to solve this I will probably understand how it works and how to use it, but now I really have no clue.


I suggest you use json_encode() as you have several issues in your created json object string.

<?php

$json_image_list = array();
foreach ($imageList as $img){
    $json_image_list[] = array(
        'thumb' => "media/img/t/$img",
        'image' => "media/img/m/$img",
    );
}

echo json_encode($json_image_list);

?>

So you know, here are a few problems in your json:

  1. An array has to be in brackets ('[' & ']').
  2. Your object properties must be wrapped in double quotes.

Also, see the bottom of JSON.org for packages that are already written for the languages you'll be using on the web so you don't have to write your own.


The JSON is invalid, don't write your own serializer, perfectly good ones exist already.


Don't build your own json. use json_encode($some_php_data_structure) to do it all for you. Otherwise you have to all the escaping/bracketing yourself, and at some point you'll miss something and end up with bad javascript, which will kill all of your client-side scripts.

All you have to do is build a data structure in PHP, pass it to json_encode(), and you're done.


The recommended way is to use json_encode. It does all the work for you and translates PHP variables into JSON strings.


The generated string is not valid JSON. All those objects should be in an array, and the name of the properties should be quoted. Why don't you use json_encode?


You can populate a normal array in PHP, then do this to echo back the JSON:

echo json_encode($your_array);

Then you don't need to worry about the JSON syntax until you're parsing it.


In response to the submitter's comment above:

Okey, this part seems to be done, but how do I recieve this data in to array inside JS? Using jQuery ofc..

You can use jQuery's getJSON function to receive JSON into a JavaScript array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜