Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am perplexed and cannot figure out why this JSON object code is not coming out like I think it should.

For reasons not important to theis example, the PHP array is created from another array. As a PHP array, this works fine.

for($i=0;$i<$FileCount;$i++)
{
    $FileList[$i] = array
    (
        "Category" => $Category, "FileName" => $FileName, "Ext" => $Ext, "Title" => $Title, "ShortName" => $ShortName
    );
}

print_r($FileList);
Array ( [0] => Array ( [Category] => AntiqueGlass [FileName] => AntiqueGlass-BlackGlassBox-B [Ext] => jpg [Title] => BlackGlassBox [ShortName] => AntiqueGlass-BlackGlassBox ) [1] => Array ( [Category] => AntiqueGlass [FileName] => AntiqueGlass-BluePicture-B [Ext] => jpg [Title] => BluePicture [ShortName] => AntiqueGlass-BluePicture ) )

I then do this, with the result of the write shown underneath

$json_array=json_encode($FileList);
echo('
<script type="text/javascript">
    var JSFileList = '.json_encode($json_array, JSON_FORCE_OBJECT).';
    document.write(JSFileList);
</script>'."\n");

[{"Category":"AntiqueGlass","FileName":"AntiqueGlass-BlackGlassBox-B","Ext":"jpg","Title":"BlackGlassBox","ShortName":"AntiqueGlass-BlackGlassBox"},{"Category":"AntiqueGlass","FileName":"AntiqueGlass-BluePicture-B","Ext":"jpg","Title":"BluePicture","ShortName":"AntiqueGlass-BluePicture"}]

I believe the result should come out something like this

{"0":("Category":"AntiqueGlass","FileName":"AntiqueGlass-BlackGlassBox-B","Ext":"jpg","Title":"BlackGlassBox","ShortName":"AntiqueGlass-BlackGlassBox")},{"1":("Category":"AntiqueGlass","FileName":"AntiqueGlass-BluePicture-B","Ext":"jpg","Title":"BluePicture","ShortName":"AntiqueGlass-BluePicture")}

The intent is to access the object like this

JSFileList[i].FileName

All my research says this should work. I am at a loss to know what I am doing wrong.

share|improve this question
    
Why use force object? What you ant in JavaScript is an array of objects. Just plain old json_encode will be fine if you only call it once. –  Mike Brant Feb 3 at 6:49
    
It's really unnecessary, as json_encode will force it anyway, because it is, at lease partly, an associative array. –  RationalRabbit Feb 3 at 6:53
add comment

3 Answers

up vote 1 down vote accepted

You are calling json_encode twice:

$json_array=json_encode($FileList);
//.....
json_encode($json_array, JSON_FORCE_OBJECT);

you must use the JSON_FORCE_OBJECT-option in the first call, after that $json_array already is a String, JSON_FORCE_OBJECT will have no effect there.

Of course the resulting variable JSFileList with the double-encoding will also be a string, when you need to access the object in JS encode it only 1 time(document.write then would print something like [object Object]).

share|improve this answer
    
+1 I overlooked that problem. –  deceze Feb 3 at 6:40
    
As did I, obviously (grin). Got lost somewhere in my experimenting ... And that was the problem. –  RationalRabbit Feb 3 at 6:48
add comment
[{...},{...},{...}]

This is an array of objects. You can access the individual objects by the numeric array index, like JSFileList[2].FileName. It already works as you need it.

Javascript distinguishes between continuously numerically indexed arrays ([]) and associative objects ({}), PHP does not. json_encode encodes all continuously numerically indexed PHP arrays to Javascript arrays, anything else to objects.

share|improve this answer
    
The result of JSFileList[0].FileName is [. The result of JSFileList[1].FileName is (. The result of JSFileList[2].FileName is ". –  RationalRabbit Feb 3 at 6:31
    
Cannot reproduce. jsfiddle.net/VQHtp –  deceze Feb 3 at 6:38
    
CORRECTION: The result of JSFileList[0].FileName is undefined. The result of JSFileList[0] is [ . The result of JSFileList[1] is (, etc. –  RationalRabbit Feb 3 at 6:40
    
Actually yes, because of stackoverflow.com/a/21521138/476. Combine that and my answer. –  deceze Feb 3 at 6:41
add comment

It is an array of objects. You are not able to access JSFileList[i].FileName because they are in string form. You need to use JSON.parse().

echo('
<script type="text/javascript">
    var JSFileList = JSON.parse('.json_encode($json_array, JSON_FORCE_OBJECT).');
    document.write(JSFileList[0].FileName);
</script>'."\n");
share|improve this answer
    
Why the downvote? Am I wrong? –  Kamehameha Feb 3 at 6:19
    
No, he's not outputting it as a string in Javascript, it's an object/array literal in the JS source. –  deceze Feb 3 at 6:20
    
Ah... but when I echo json_encode() from PHP, and when I use it as an input in javaScript, won't it be text? I tried it out, and i am able to access JSFileList[i].FileName from Javascript. –  Kamehameha Feb 3 at 6:36
    
You're echoing Javascript source code as text with JSON as text embedded in it... If the resulting source code looks like jsfiddle.net/VQHtp it's not text to Javascript. –  deceze Feb 3 at 6:39
    
Yes, I am echoing out JS as text,since it was in the question.The asker has said that and he intends to use JSFileList[i].FileName after performing the above said operation. So doesn't it solve the purpose? Sorry if I am being thick... –  Kamehameha Feb 3 at 6:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.