我设置了一个JsonRestStore,如下所示:
var store = new JsonRestStore({target:"http://localhost:****/blub/"});
我通过以下方式获取数据:
store.get("rulesets");
服务器在控制台中返回:
{"xomName":"loaded","topicName":"Alpha","ioobjectName":"class1","rulesetName":"xmlbinding$45$rules"
},{"xomName":"loaded","topicName":"Beta","ioobjectName":"class2","rulesetName":"xmlbinding$45$rules"
},{"xomName":"loaded","topicName":"gammp","ioobjectName":"class3","rulesetName":"xmlbinding$45$rules"
}]
所以现在我想解析整个商店(规则集)。 我的建议是这样做:
var memo = new Memory();
memo = JSON.parse(JSON.stringify(store.get("rulesets")));
console.log(memo);
而且,正如假设的那样,它根本不起作用。
所以我的问题是:
有没有办法解析整个商店? 这将是惊人!
非常感谢!
最佳答案
您的商店将结果作为对象返回。如果要将对象转换为字符串,则需要使用 JSON.stringify
:
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
如果你想再次将该字符串转回对象,则需要使用 JSON.parse:
The JSON.parse() method parses a string as JSON, optionally transforming the value produced by parsing.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
你在这里做什么:
JSON.parse(JSON.stringify(store.get("rulesets")));
正在使用 JSON.stringify
将 store.get("rulesets")
的结果对象转换为字符串,然后将结果字符串转回使用 JSON.parse 再次转换为对象。因此,事实上整条线不执行任何操作/自行撤消。
正确的例子:
// Assign result object to "myObject" variable
var myObject = store.get("rulesets");
// Convert "myObject" to string and assign to variable "myString"
var myString = JSON.stringify(myObject);
// Convert "myString" to object and assign to variable "myOtherObject"
var myOtherObject = JSON.parse(myString);
关于javascript - Dojo 如何解析整个 JsonRestStore(JSON 到字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35730314/