JSON.parse() driving me nuts. (PHP, javascript related)

Sep 29, 2004
18,665
67
91
I have a problem using this. It just seems to not work.

I am using AJAX (YUI2 API for it) to talk to the server. The server runs a PHP script.
In PHP, I essentially do this:
Code:
header('Content-type: application/json'); // needed so JSON.parse() in javascript knows how to handle the response
echo "{\"101\": \"ABC\"}";

So, I have verified through FireBug that I am getting this string from responseText when running my javascript code:
Code:
{"101": "ABC"}

Then I do this expecting to get the javascript array tempArrayObject['101'] = 'ABC':
Code:
<script>
// some code

var tempArrayObject = new Array();
var jsonParse = JSON.parse(o.responseText); // responseText is the string in the previous code block
tempArrayObject.push(jsonParse);
var selection = document.getElementById("someCmbobox");
for(var index in tempArrayObject) {
    selection.options[selection.options.length] =
    new Option(tempArrayObject[index], index, false, true);
}

// some code
</script>

the call to JSON.parse() seems to not be working. I have spent two hours on this and I am at a total loss. Pretty close to trying to use the YUI2 provided solution even though their comments say that if JSON.parse is available that their code simply calls it. Probably a good idea for legacy support but the current problem still is something I want to understand.
 
Last edited:

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
Just quickly glossing over your code, I'd expect tempArrayObject[0] = jsonParse.

You are "pushing" the javascript object into the first element of tempArrayObject.
 
Sep 29, 2004
18,665
67
91
My understanding may be wrong. But ....

JSON.parse(o.responseText);

returns a Javascript object. And this ...

tempArrayObject.push(jsonParse);

takes that object and converts it to an array. Meaning that this line is unneeded? ...

var tempArrayObject = new Array();

This is very confusing to me. This seems like simple enough code to deal with. I could use the eval() method but people say there are security issues with that granted I don't know what they could possibly be since this all runs client side.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
array.push(something) does not convert something to an array. It adds something as an element of array.

json.parse() returns a javascript object or an array depending on the json text.

Your text will return an object because of your identifier.

so

array.push(json.parse(something)) will add an object to your array.
 
Sep 29, 2004
18,665
67
91
array.push(something) does not convert something to an array. It adds something as an element of array.

json.parse() returns a javascript object or an array depending on the json text.

Your text will return an object because of your identifier.

so

array.push(json.parse(something)) will add an object to your array.

Could you clarify what you mean by identifier?

What do I have to do to populate my array so that I have this in the end:
tempArrayObject[101] = "ABC";

In my mind, your solution from post #2 woudl give me this:
tempArrayObject[0]=?????

I jsut don't see what that would do.

Once i get this figured out, I might have more questions regarding how to deal with more complicated JSON formatted responses.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
The identifier is referring is the structure of your JSON text.

You've got {"101": "ABC"} , your identifier is "101"
If you had {"101","123","ABC"}, JSON.parse() will return an array.

So let's breakdown your code as an example. The JSON.parse() method produces an object.

Code:
// create JSON
var jsonText = '{"101":"ABC"}';
var parseJson = JSON.parse(jsonText);
 
// display JSON
document.write(parseJson.101); // outputs ABC

I'm assuming when you say you want tempArrayObject[101] = "ABC" that you want to produce an associative array. In that case, you don't need to do anything other than to create the JSON object.

Code:
// create JSON
var jsonText = '{"101":"ABC"}';
 
// display JSON
document.write(jsonText["101"]); // outputs ABC

If you WEREN'T looking to make an associative array and want to create an array at the INDEX of 101 with the value of "ABC", you're gonna have to set a size equal or greater than 101, then set that array index with "ABC." You cannot automagically do this with JSON.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,295
3,920
75
For what you actually want, I'd expect tempArrayObject = jsonParse.
 
Sep 29, 2004
18,665
67
91
Ya, I need an associative array. I am going to have index like 1, 101, 999. Could even be 1000000. Trying out the ideas mentioned right now.
 
Sep 29, 2004
18,665
67
91
KIAman and Ken_g6,

Thanks! I finally got it working. I hate examples found on the internet! But atleast the internet got me 90&#37; of the way there. So frustrating that I could not find good documentation explaining what JSON.parse() actually does. Or atleast a good article on it. For backwards compatability (old browsers), I still might need to use the YUI 2 API.

ya, JSON.parse() returns an actual javascript Array object.
 
Sep 29, 2004
18,665
67
91
Oh, If "ABC" has quotes in it ("some"String"), that means I have to URL encode it right? I do see that JSON.parse() takes a second argument that could be the decoder for it.
 
Sep 29, 2004
18,665
67
91
Oh, If "ABC" has quotes in it ("some"String"), that means I have to URL encode it right? I do see that JSON.parse() takes a second argument that could be the decoder for it.

Also, YUI2's interface for parsing is the exact same interface as JSON.parse():
http://developer.yahoo.com/yui/json/