JSON点表示法转换为字符串 [英] JSON dot notation to string

查看:120
本文介绍了JSON点表示法转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中使用JSON,并且试图获取点表示法的字符串值.

I am using JSON within my javascript and I am trying to get a string value for the dot notation representation.

例如AAA.BBB[0].CCC.DDD[5].EEE = 123(JSON点表示法格式).

For example AAA.BBB[0].CCC.DDD[5].EEE = 123 in JSON dot notation format.

但是我想将值AAA.BBB[0].CCC.DDD[5].EEE作为字符串来获取,因此我可以将其保存以供以后使用,以便允许我直接修改JSON代码.

But I want to get the value AAA.BBB[0].CCC.DDD[5].EEE as a String so I can save it for later use to allow me to modify my JSON code directly.

Javascript或jQuery中是否有可以返回字符串值表示形式的方法?

Is there a method in Javascript or jQuery that can return a string value representation?

*编辑 我正在将JSON数据转换为列表结构,并且我想将AAA.BBB[0].CCC.DDD[5].EEE格式保存为id,因此当用户修改该列表项的内容时,它将直接修改JSON数据.会有更好的方法将位置保存在id中吗?

*EDIT I am converting JSON data into a list structure, and I want to save the AAA.BBB[0].CCC.DDD[5].EEE format as the id so when a user modifies the content of that list item it modifies the JSON data directly. Would there be a better way to save the location in the id?

推荐答案

为什么不只存储对其内部部分的引用?将其存储为字符串没有多大意义.如果您只想使用一种更短的访问方式,则存储引用更为合理.

Why not just store a reference to an inner-portion of it? Storing it as a string doesn't make much sense. If you just want a shorter way of accessing it, storing a reference makes more sense.

var theCollection = AAA.BBB[0].CCC.DDD[5];
alert( theCollection.EEE );

此引用随后可以存储在元素上,以后可以检索.

this reference can then be stored on an element and retrieved later.

$(someelement).data("jsonref",theCollection);
var data = $(someelement).data("jsonref");
alert(data.EEE);
data.EEE = "foobar";
data = $(someelement).data("jsonref");
alert( data.EEE );

这篇关于JSON点表示法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆