PDA

View Full Version : Trouble with getting JSON response into a variable



FlaminPhoenix
28 Dec 2010, 10:06 PM
Hi all, though the title of the thread looks trivial, Im struggling from the past 2 hours to get this working.

Now, Im working on integrating a couple of components into a webpage, and one of them returns a JSON string (from a web service) when the user selects anything on it. This is a third party component with no exposed source. I have a function exposed on the webpage which accepts a parameter, and the function is being called correctly from the third party component. Now, the json variable that my function is getting is acting strange!! Im able to alert the json, and log to the console, but when I use Ext.util.JSON.decode(), theres an error thrown "Expected ';' ". Problem is, even if I try to assign this json string to another variable / inspect it, I get a blank string!!! Im at wits end here, and am running out of ideas.

As I mentioned earlier, I dont have control on the third party component. Can someone help me here?

FlaminPhoenix
28 Dec 2010, 10:10 PM
And, no, I dont have a problem with understanding AJAX calls are asynchronous. As already mentioned, only after the user does an action on component 2, and a server request is made, does my function get called.

mankz
28 Dec 2010, 11:36 PM
1. Try running the returned JSON in a JSON validator. http://www.jsonlint.com/
2. Post it here.

FlaminPhoenix
29 Dec 2010, 1:19 AM
Valid JSON

mankz
29 Dec 2010, 1:21 AM
2. Post it here.
:)

FlaminPhoenix
29 Dec 2010, 1:53 AM
the JSON? I dont think I can.. what with my third party saying itll leak out trade secrets and the like 8-|

mankz
29 Dec 2010, 1:59 AM
Hehe, right. :) Hard to help without it though...

FlaminPhoenix
29 Dec 2010, 2:02 AM
can I PM you the JSON?

mankz
29 Dec 2010, 2:03 AM
Sure!

FlaminPhoenix
29 Dec 2010, 2:04 AM
Hey Mankz, I have PM'd you the JSON, could you please take a look and tell me what I am doing wrong?

mankz
29 Dec 2010, 2:07 AM
Seems like what you have is already a valid javascript object that doesn't need to be decoded...?

FlaminPhoenix
29 Dec 2010, 2:11 AM
How can that be??!! When I do an inspect, and add "typeof jsonvar" into the watch window, I get "string". If it really is an object, it should show up as one in the inspection tool in IE right?

EDIT: If what you guessed was right, then I should be able to do a "eval(jsonstr)" with no errors right? I DO get the "expected ';' " error.

mankz
29 Dec 2010, 2:16 AM
Ok, the PM you sent didn't look like a string. Just like a big array...?

FlaminPhoenix
29 Dec 2010, 2:17 AM
yes. My point exactly. How do I use this in my javascript.

mankz
29 Dec 2010, 2:18 AM
So is your variable a string or an array then? :)

FlaminPhoenix
29 Dec 2010, 2:37 AM
Hey mankz I figured it out!!! Im actually getting an object array instead of a JSON string.. wonder why the third party dudes called it a JSON string x_x ... But I discovered another interesting fact.. why does the typeof on an object return a "string" ??!!!strange...

mankz
29 Dec 2010, 2:40 AM
Something's definitely weird. If typeof says "string", it's a string...

FlaminPhoenix
29 Dec 2010, 2:50 AM
but when i do jsonstr.length, it behaves like an array and gives me the length in it.

mankz
29 Dec 2010, 3:04 AM
Strings also have a 'length' property. You can test if it is an array by



yourVar instanceof Array

FlaminPhoenix
29 Dec 2010, 3:11 AM
I get back a "false". In all this running around I completely forgot the string object also has a "length" defined under it.. my bad.. Then its not an object array at all, but a string!!! back where I started huh?!

Condor
29 Dec 2010, 4:13 AM
You can test if it is an array by



yourVar instanceof Array


Actually, that's also not fool-proof, because 'Array' is local to the current frame. You will get false as a result if you do this with an array created by another frame.

The correct method (which is also the one used by Ext.isArray) is:

Object.prototype.toString.call(v) === '[object Array]'

Note: Ext also has an isIteratable() method to check for any Array-like objects (like arguments, NodeList, HTMLCollection etc.).