应用于 JSON 字符串时 angular.fromJson 和 $scope.$eval 之间的区别 [英] Difference between angular.fromJson and $scope.$eval when applied to JSON string

查看:18
本文介绍了应用于 JSON 字符串时 angular.fromJson 和 $scope.$eval 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 angularjs 应用程序中,我通常使用 angular.fromJson 解析 JSON 字符串,如下所示:

var myObject=angular.fromJSON(jsonString);

但是,使用 $scope.$eval 似乎可以得到相同的结果:

var myObject=$scope.$eval(jsonString);

看这个小提琴

或者使用 vanilla javaScript,像这样:

var myObject=JSON.parse(jsonString);

  • 使用 angular.fromJSON 而不是 JSON.parse 有什么特别的理由吗?

  • 使用 $scope.$eval 解析 JSON 字符串时是否有任何可能的问题?

解决方案

查看 源代码:

function fromJson(json) {返回 isString(json)?JSON.parse(json):json;}

它们只是传递到 JSON.parse.

至于 $eval它指向 $parse:

//$scope.$eval 源代码:$eval: 函数(expr, locals) {返回 $parse(expr)(this, locals);},

$parse source 太长无法发布,但它本质上能够将内联(字符串化)对象转换为 real 对象,因此在这种情况下,它实际上会转换您的 JSON

(直到刚刚阅读 $parse 源时我才知道这一点.)

<块引用>

使用 angular.fromJSON 而不是 JSON.parse 有什么特别的理由吗?

不,不是真的.尽管他们确实会检查您以确保您不会重复解析 JSON 字符串,如下所示:

var jsonString = '{"foo":"bar"}';var json = JSON.parse(jsonString);//解析一次很好:)JSON.parse(json);//解析两次很糟糕:(

<块引用>

在使用 $scope.$eval 解析 JSON 字符串时是否有任何可能的问题?

我不以为然,除了您所做的工作比必要的多.所以如果你知道你有 JSON,就没有理由使用更重的 $parse 函数.

In my angularjs apps, I usually parse a JSON string by using angular.fromJson, like so:

var myObject=angular.fromJSON(jsonString);

However, it seems that I would obtain the same result by using $scope.$eval:

var myObject=$scope.$eval(jsonString);

See this fiddle

Or by using vanilla javaScript, like so:

var myObject=JSON.parse(jsonString);

  • Is there any particular reason to use angular.fromJSON rather than JSON.parse?

  • Is there any possible issue when using $scope.$eval to parse a JSON string?

解决方案

Check out the source code:

function fromJson(json) {
  return isString(json)
      ? JSON.parse(json)
      : json;
}

They're just passing through to JSON.parse.

As for $eval it shells out to $parse:

  // $scope.$eval source:
  $eval: function(expr, locals) {
    return $parse(expr)(this, locals);
  },

$parse source is too long to post, but it is essentially capable of converting inline (stringified) objects to real Objects and so it makes sense that in this case, it will actually convert your JSON as well.

(I did not know this until reading through the $parse source just now.)

Is there any particular reason to use angular.fromJSON rather than JSON.parse?

Nope, not really. Although they do check to you to ensure that you don't double-parse a JSON string, like so:

var jsonString = '{"foo":"bar"}';
var json = JSON.parse(jsonString); // Parsing once is good :)
JSON.parse(json); // Parsing twice is bad :(

Is there any possible issue when using $scope.$eval to parse a JSON string?

I don't think so off the top of my head, other than that you're doing more work than is necessary. So if you know you have JSON, there's no reason to use the heavier $parse function.

这篇关于应用于 JSON 字符串时 angular.fromJson 和 $scope.$eval 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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