我想在HTML5中存储JavaScript对象localStorage,但我的对象显然正在转换为字符串.
我可以使用存储和检索原始JavaScript类型和数组localStorage,但对象似乎不起作用.他们应该吗?
这是我的代码:
var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
console.log(' ' + prop + ': ' + testObject[prop]);
}
// Put the object into storage
localStorage.setItem('testObject', testObject);
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);
Run Code Online (Sandbox Code Playgroud)
控制台输出是
typeof testObject: object
testObject properties:
one: 1 …Run Code Online (Sandbox Code Playgroud) 我在我的javascript中做一个console.log语句,以便记录一个javascript对象.我想知道是否有办法,一旦完成 - 将该对象复制为javascript代码.我要做的是转换使用ajax创建的对象将xml提要解析为静态javascript对象,以便文件可以在本地运行,而无需服务器.我在chrome检查器窗口中包含了对象的屏幕截图,以便您可以看到我正在尝试做的事情.
我有以下......
chrome.extension.sendRequest({
req: "getDocument",
docu: pagedoc,
name: 'name'
}, function(response){
var efjs = response.reply;
});
Run Code Online (Sandbox Code Playgroud)
它调用以下..
case "getBrowserForDocumentAttribute":
alert("ZOMG HERE");
sendResponse({
reply: getBrowserForDocumentAttribute(request.docu,request.name)
});
break;
Run Code Online (Sandbox Code Playgroud)
但是,我的代码永远不会到达"ZOMG HERE",而是在运行时抛出以下错误 chrome.extension.sendRequest
Uncaught TypeError: Converting circular structure to JSON
chromeHidden.JSON.stringify
chrome.Port.postMessage
chrome.initExtension.chrome.extension.sendRequest
suggestQuery
Run Code Online (Sandbox Code Playgroud)
有谁知道是什么原因引起的?
我有一个对象(解析树),其中包含对其他节点的引用的子节点.
我想序列化这个对象,使用JSON.stringify(),但我得到:JSON.stringify()因为我提到的结构.
我怎么能解决这个问题?对于我来说,序列化对象中是否表示了对其他节点的这些引用并不重要.
另一方面,在创建对象时从对象中删除这些属性似乎很乏味,我不想对解析器(narcissus)进行更改.
情况:我有一个包含多个子和子子对象的大对象,其属性包含多种数据类型.出于我们的目的,此对象看起来像这样:
var object = {
aProperty: {
aSetting1: 1,
aSetting2: 2,
aSetting3: 3,
aSetting4: 4,
aSetting5: 5
},
bProperty: {
bSetting1: {
bPropertySubSetting : true
},
bSetting2: "bString"
},
cProperty: {
cSetting: "cString"
}
}
Run Code Online (Sandbox Code Playgroud)
我需要循环遍历此对象并构建显示层次结构的键列表,因此列表最终看起来像这样:
aProperty.aSetting1
aProperty.aSetting2
aProperty.aSetting3
aProperty.aSetting4
aProperty.aSetting5
bProperty.bSetting1.bPropertySubSetting
bProperty.bSetting2
cProperty.cSetting
Run Code Online (Sandbox Code Playgroud)
我有这个函数,它循环遍历对象并吐出密钥,但不是分层次的:
function iterate(obj) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object") {
iterate(obj[property]);
}
else {
console.log(property + " " + obj[property]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我怎么做吗?这里有一个jsfiddle让你搞砸:http: …
我想将DOM节点甚至整个序列化为windowJSON.
例如:
>> serialize(document)
-> {
"URL": "http://stackoverflow.com/posts/2303713",
"body": {
"aLink": "",
"attributes": [
"getNamedItem": "function getNamedItem() { [native code] }",
...
],
...
"ownerDocument": "#" // recursive link here
},
...
}
Run Code Online (Sandbox Code Playgroud)
JSON.stringify(window) // TypeError: Converting circular structure to JSON
Run Code Online (Sandbox Code Playgroud)
问题是JSON默认不支持循环引用.
var obj = {}
obj.me = obj
JSON.stringify(obj) // TypeError: Converting circular structure to JSON
Run Code Online (Sandbox Code Playgroud)
window和DOM节点有很多.window === window.window将如此document.body.ownerDocument === document.
此外,JSON.stringify不序列化函数,所以这不是我想要的.
`dojox.json.ref.toJson()` can easily serialize object with …Run Code Online (Sandbox Code Playgroud) JSON.stringify(eventObject);
得到:
TypeError: Converting circular structure to JSON
dojox.json.ref.toJson(eventObject);
得到:
TypeError: Accessing selectionEnd on an input element that cannot have a selection.
是否有一些库/代码可以用来完成它?
我想存储(本地存储 HTML5)JS 对象。为此,我必须应用JSON.stringify(obj)到我想要存储的 JS 对象。在此之后,我可以存储对象localStorage.obj=JSON.stringify(obj);
但是一些 JS 对象非常大并且包含圆形结构。为了处理圆形结构,我发现了以下两种方法:
因为这两种方法都不能满足我的要求,所以我再次问这个常见问题。有没有人知道一种保存包含圆形结构的 JS 对象并加载这些对象的方法,以便它们与之前存储时的对象完全相同(具有所有圆形结构)?
我有以下映射:
public class TimeLineEntityMap : BaseEntityMap<TimeLineEntity>
{
public TimeLineEntityMap()
{
Table("time_line_entity");
Map(x => x.Message);
Map(x => x.ResearchId, "research_id");//.Cascade.All().Not.LazyLoad();
ReferencesAny(x => x.EntityRef)
.AddMetaValue<EmailEntity>(typeof(EmailEntity).Name)
.AddMetaValue<UrlEntity>(typeof(UrlEntity).Name)
.AddMetaValue<PhoneEntity>(typeof(PhoneEntity).Name)
.EntityTypeColumn("entity_type")
.IdentityType<long>()
.EntityIdentifierColumn("entity_ref_id")
.Not.LazyLoad();
}
}
Run Code Online (Sandbox Code Playgroud)
从数据库中获取时EntityRef作为代理。
TimeLineEntity res = timeLineRepository.Find(x => x.Id == id);
JsonConvert.SerializeObject(res);
Run Code Online (Sandbox Code Playgroud)
在JsonConvert抛出:
Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'ManifestModule' with type 'System.Reflection.RuntimeModule'. Path 'Data[0].EntityRef._proxyFactoryInfo._getIdentifierMethod.Module.Assembly'.
Run Code Online (Sandbox Code Playgroud)
这是我的json设置:
x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
x.SerializerSettings.ContractResolver = new NHibernateContractResolver();
Run Code Online (Sandbox Code Playgroud)
public class NHibernateContractResolver : CamelCasePropertyNamesContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将带有循环引用的对象从node.js服务器传递到客户端javascript.
服务器(node.js):
var object = { circular: object }
//....
app.get('/', function(req, res){
res.render('index.jade', {object: object});
});
Run Code Online (Sandbox Code Playgroud)
客户端Jade/Javascript
script var object = !{JSON.stringify(object)};
Run Code Online (Sandbox Code Playgroud)
在这里,我得到object包含循环引用的错误.
有没有
循环引用的任何方式来获取object客户端javascript ?