Login Register

Move from 4.x to 9-- syntax for converting JSON string to JS array with DOJO

Hello,

I am trying to read JSON string into a dojo Object. How I can do it in 9. In 4 I did

require_once("dojo.json");
var object=dojo.json.evalJson(data)

but I think now dojo does not have even have dojo.json. I have seen the porting guide but I could not make much sense out of it. What is FileItemReadStore? I could not find about it anywhere in the source code. And also what do you mean by changing JSON syntax? I am guessing its the way to read json array. But first of all how should I convert a json string to java script array? An example might be helpful.

sorry its

sorry its

dojo.require("dojo.json").

typical json....

can be converted from JSON to object via var myJSONobject = eval(jsonString); Here is what evalJson did...
evalJson: function(/*String*/ json){
                // summary:
                //           evaluates the passed string-form of a JSON object
                // json:
                //            a string literal of a JSON item, for instance:
                //                  '{ "foo": [ "bar", 1, { "baz": "thud" } ] }'
                // return:
                //            the result of the evaluation
                // FIXME: should this accept mozilla's optional second arg?
                try {
                        return eval("(" + json + ")");
                }catch(e){
                        dojo.debug(e);
                        return json;
                }
        },
Basically it wrapped the eval in a try/catch incase the json string wasn't valid object notation.....
-Karl

Which is now just

Which is now just dojo.fromJson, right?

Correct

I forgot the page I was looking at doesnt include all the folders from SVN, so I have to search dojo.js for those things.

dojo.fromJson()
and dojo.toJson() are the primary JSON functions.

Guess that needs to be in the porting guide... wonder who didnt do their job properly...

-Karl

I did more digging into the

I did more digging into the code for dojo9. under /dojo-0.9.0beta/dojo/data/ which seems to have a JsonItemStore.js but I could not find anything that has been implemented. I think what I am looking for is dojo.data.api.Read.getValues
Am I right now?

Also can someone tell me what's this http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-...
is about?

JSON is implemented in dojo

JSON is implemented in dojo _base directory... (part of dojo.js)

The ItemFile*Store question should be a new topic as you will more likely get input from the data people that way.

-Karl

porting guide page

http://dojotoolkit.org/book/dojo-porting-guide-0-4-x-0-9/dojo-json
(formating is still foobar but info is there)
-Karl

Thanks Karl, I tried both

Thanks Karl,

I tried both ways using JS native eval(("(" + jsonString + ")") and dojo.fromJson("jsonString") and I got the same kind of error using Firebug. Basically my eval is failing. I dont think the jsonString is in a wrong format because the same string is good for dojo4.

Now I think my problem lies in how I am getting the data from dojo.xhrGet. Can you help me with that? I am doing this

function createTable() {
//Gets the data aysynchronously
dojo.xhrGet({
url: "../statusviewer/maintable.php",
handleAs:"json",
mimetype:"text/json" ,
timeout: 500,
load: foo,
content: {
dates: document.getElementById('foo').innerHTML,
events:document.getElementById('bar').innerHTML
}

});
}

Sorry if I am asking more. Thanks anyways

with handleAs...

your data should be pre-evaluated by xhrGet.... as seen in this code....
dojo._contentHandlers = {
	"text": function(xhr){ return xhr.responseText; },
	"json": function(xhr){ 
		console.debug("please consider using a mimetype of text/json-comment-filtered to avoid potential security issues with JSON endpoints");
		return dojo.fromJson(xhr.responseText);
	},
	"json-comment-optional": function(xhr){ 
		// NOTE: we provide the json-comment-filtered option as one solution to
		// the "JavaScript Hijacking" issue noted by Fortify and others. It is
		// not appropriate for all circumstances.
		var value = xhr.responseText;
		var cStartIdx = value.indexOf("\/*");
		var cEndIdx = value.lastIndexOf("*\/");
		if((cStartIdx == -1)||(cEndIdx == -1)){
			return dojo.fromJson(xhr.responseText);
		}
		return dojo.fromJson(value.substring(cStartIdx+2, cEndIdx));
	},
	"json-comment-filtered": function(xhr){ 
		// NOTE: we provide the json-comment-filtered option as one solution to
		// the "JavaScript Hijacking" issue noted by Fortify and others. It is
		// not appropriate for all circumstances.
		var value = xhr.responseText;
		var cStartIdx = value.indexOf("\/*");
		var cEndIdx = value.lastIndexOf("*\/");
		if((cStartIdx == -1)||(cEndIdx == -1)){
			// FIXME: throw exception instead?
			console.debug("your JSON wasn't comment filtered!"); 
			return "";
		}
		return dojo.fromJson(value.substring(cStartIdx+2, cEndIdx));
	},
	"javascript": function(xhr){ 
		// FIXME: try Moz and IE specific eval variants?
		return dojo.eval(xhr.responseText);
	},
	"xml": function(xhr){ 
		return xhr.responseXML;
	}
};
-Karl

Sorry for the trouble the

Sorry for the trouble the problem is its not

load:foo

it should be

handle:foo.

i think it's actually

i think it's actually handleAs: foo

From my last reply...

Basically you should be able to do data.somePropertyNameFromJSONdata... no need to re-evaluate.

-Karl