如何使用 Javascript 解析具有内层的 JSON?

发布于 2024-10-08 02:06:25 字数 380 浏览 0 评论 0 原文

我可以使用 javascript 评估简单的 JSON。

var json = '{"amount":"50","id":"3"}';
var out = eval("{" + json + "}");

现在我将 JPA 与 REST 一起使用,JSON 化的查询结果将包含表名,这使得 JSON 具有内部 JSON,所以简单的 eval 是行不通的。

{"inventory":{"amount":"50","id":"3"}}

我在网上寻找解决方案,但找不到我的案例。 我应该只进行字符串操作并提取 {"amount":"50","id":"3"} 部分吗? 或者还有其他办法吗?

I can eval simple JSON with javascript.

var json = '{"amount":"50","id":"3"}';
var out = eval("{" + json + "}");

Now I am using JPA with REST and JSON-nized query result would include table name which makes
JSON having inner JSON so simple eval wouldn't work.

{"inventory":{"amount":"50","id":"3"}}

I've looked around the web for solution but can't find my case.
Should I just do string manipulation and extract {"amount":"50","id":"3"} part?
Or is there other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟沫凡尘 2024-10-15 02:06:26

即使这样也应该有效:

var json = '{"inventory":{"amount":"50","id":"3"}}';
var out = eval("{" + json + "}");

alert(out.inventory.amount);

但最好使用 JSON.parse

Even this should work:

var json = '{"inventory":{"amount":"50","id":"3"}}';
var out = eval("{" + json + "}");

alert(out.inventory.amount);

But better to use JSON.parse

ぇ气 2024-10-15 02:06:26

无论如何,我认为执行简单 eval 的正确方法是将 json 字符串用括号括起来,而不是大括号...

var out = eval("(" + json + ")");

比照。 https://github.com/douglascrockford/JSON-js/blob/master /json.js

// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.

                j = eval('(' + text + ')');

Aniway, I think that the proper way to perform a simple eval is to have the json string surrounded with parenthesis, not curly brackets...

var out = eval("(" + json + ")");

Cf. https://github.com/douglascrockford/JSON-js/blob/master/json.js :

// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.

                j = eval('(' + text + ')');
记忆で 2024-10-15 02:06:25

是的,还有另一种(更好的)方法!使用 JSON.parse() 进行解析您的 JSON 并获取您的对象:

var obj = JSON.parse(jsonString);
//then, for example...
var amount = obj.inventory.amount;

对于没有本机 JSON 支持的旧版浏览器(例如 IE <8),请包含 json2.js 所以上面的方法仍然有效。

Yes, there is another (better) way! Use JSON.parse() to parse your JSON and get your object out:

var obj = JSON.parse(jsonString);
//then, for example...
var amount = obj.inventory.amount;

For older browsers (IE <8 for example) without native JSON support, include json2.js so this above still works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文