- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试在 Angular 中使用 JSON 文件,但引用文件不是真正的 JSON。访问它并将其解析为真正的 JSON 并使用的最佳方法是什么?
我正在尝试在 angular.js 的 Controller 中访问 notTrue.json,如下面的文件结构所示。并将 notTrue.json 解析为真正的 JSON 格式。
文件结构示例:
index.html
js (folder)
- angular.js
- notTrue.json
- etc
css (folder)
img (folder)
非 JSON 格式示例,notTrue.json 文件:
var Foo = [{
'bar': 'Hello World',
'subItem': {
'phone1': '(555) 543-213',
'email': 'foo@bar.com'
}
}, {
'bar': 'Goodnight Moon',
'subItem': {
'phone1': '(555) 521-3344',
'email': 'foo2@bar.com'
}
}];
我想将其转换为正确的 JSON 格式,如下所示:
[{
"bar": "Hello World",
"subitem": {
"phone1": "(555) 543-213",
"email": "foo@bar.com"
}
}, {
"bar": "Goodnight Moon",
"subitem": {
"phone1": "(555) 521-3344",
"email": "foo2@bar.com"
}
}]
最佳答案
如果这个 notTrue.json
文件是静态的,即当用户访问 index.html
时它已经存在,并且在用户使用过程中不会改变应用程序,那么最好在加载 angular.js
之前通过 script
标签包含 notTrue.json
,如下所示:
<script src="js/notTrue.json"></script>
<script src="js/angularjs"></script>
然后您将可以访问应用程序中的 Foo
对象。以下代码通过在屏幕上输出 JSON 格式来说明这一点:
<div ng-app="myApp" ng-controller="myControler">
<pre>{{json}}</pre>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myControler', function($scope, $window) {
// window.Foo was defined when loading notTrue.js
// Convert it to JSON string (pretty printed)
$scope.json = JSON.stringify($window.Foo, null, 4);
});
</script>
在上面的示例中,页面输出将是:
[
{
"bar": "Hello World",
"subItem": {
"phone1": "(555) 543-213",
"email": "foo@bar.com"
}
},
{
"bar": "Goodnight Moon",
"subItem": {
"phone1": "(555) 521-3344",
"email": "foo2@bar.com"
}
}
]
请注意,pre
标记和 JSON.stringify
方法的额外参数用于漂亮地打印 JSON 字符串。但这对于你的问题来说并不重要。
动态加载notTrue.js
另一方面,如果您希望推迟加载 notTrue.js
直到某个事件发生,因为它的内容可能会在页面加载和该事件之间发生变化,那么您可以使用 $ http.get
.
在以下示例中,单击按钮即可加载文件,并在屏幕上输出 JSON 格式:
<div ng-app="myApp" ng-controller="myControler">
<button ng-click="load('js/notTrue.json')">Load</button>
<pre>{{json}}</pre>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myControler', function($scope, $http) {
// Define what should happen on button click:
$scope.load = function(file) {
$http.get(file).then(function(response) {
// Extract from the retrieved file contents
// the part between the first "=" and
// final (optional) semi-colon
var txt = response.data.replace(/^.*?=([\S\s]*?);?\s*$/, '$1');
// Evaluate that text, adding brackets to avoid misinterpretation
// when text starts with opening brace.
var Foo = eval('(' + txt + ')');
// and convert to JSON string (pretty printed)
$scope.json = JSON.stringify(Foo, null, 4);
});
};
});
</script>
输出与第一个解决方案相同。
请注意,使用此方法,加载的文件必须驻留在同一服务器上,我了解您的情况就是如此。
仅当第一个解决方案不能满足您的需求时才采用此解决方案。
您需要添加检查并处理未找到文件、不具有预期格式等情况下的潜在错误。
最后,请小心使用eval
:仅当您信任来源时才使用它,我相信这里就是这种情况。
关于javascript - 最好使用 Angular 将非 JSON 文件更改为真正的 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34099276/
缓冲区溢出问题是众所周知的。因此,我们有幸使用标准库函数,例如 wcscat_s()。 Microsoft 的好心人已经创建了类似的安全字符串函数,例如 StringCbCat()。 但是我遇到了一个
HTTP缓存相关的问题好像是前端面试中比较常见的问题了,上来就会问什么cache-control字段有哪些,有啥区别啥的。嗯……说实话,我觉得至少在本篇来说,HTTP缓存还算不上复杂,只是字段稍
代理,其实全称应该叫做代理服务器,它是客户端与服务器之间得中间层,本质上来说代理就是一个服务器,在HTTP的链路中插入的一个中间环节,就是代理服务器啦。所谓的代理服务就是指:服务本身不生产内容,
我们在前两篇的内容中分别学习了缓存和代理,大致了解了缓存有哪些头字段,代理是如何服务于服务器和客户端的,那么把两者结合起来,代理缓存,也就是说代理服务器也可以缓存,当客户端请求数据的时候,未必一
在前面的章节,我们把HTTP/1.1的大部分核心内容都过了一遍,并且给出了基于Node环境的一部分示例代码,想必大家对HTTP/1.1已经不再陌生,那么HTTP/1.1的学习基本上就结束了。这两
我们前一篇学习了HTTP/2,相比于HTTP/1,HTTP/2在性能上有了大幅的改进,但是HTTP/2因为底层还是基于TCP协议的,虽然HTTP/2在应用层引入了流的概念,利用多路复用解决了队头
前面我们花了很大的篇幅来讲HTTP在性能上的改进,从1.0到1.1,再到2.0、3.0,HTTP通过替换底层协议,解决了一直阻塞性能提升的队头阻塞问题,在性能上达到了极致。 那么,接下
上一篇噢,我们搞明白了什么是安全的通信,这个很重要,特别重要,敲黑板!! 然后,我们还学了HTTPS到底是什么,以及HTTPS真正的核心SSL/TLS是什么。最后我们还聊了聊TLS的实
经过前两章的学习,我们知道了通信安全的定义以及TLS对其的实现~有了这些知识作为基础,我们现在可以正式的开始研究HTTPS和TLS协议了。嗯……现在才真正开始。 我记得之前大概聊过,当
这一篇文章,我们核心要聊的事情就是HTTP的对头阻塞问题,因为HTTP的核心改进其实就是在解决HTTP的队头阻塞。所以,我们会讲的理论多一些,而实践其实很少,要学习的头字段也只有一个,我会在最开始
我们在之前的文章中介绍HTTP特性的时候聊过,HTTP是无状态的,每次聊起HTTP特性的时候,我都会回忆一下从前辉煌的日子,也就是互联网变革的初期,那时候其实HTTP不需要有状态,就是个浏览页面
前面几篇文章,我从纵向的空间到横向的时间,再到一个具体的小栗子,可以说是全方位,无死角的覆盖了HTTP的大部分基本框架,但是我聊的都太宽泛了,很多内容都是一笔带过,再加上一句后面再说就草草结束了。
我的问题确实很简单,是否应该对适配器(设计模式)类进行单元测试,以及如何进行测试? 例子: 我想用PHP创建一个ClientSocket类,它是fsockopen,fread,fwrite的适配器。
目前,我在 PHP 脚本中使用此查询: SELECT * FROM `ebooks` WHERE `id`!=$ebook[id] ORDER BY RAND() LIMIT 125; 数据库最多大约
我们都知道可以使用 GetCustomAttributes 方法查询程序集的属性。我想用它来识别我的应用程序的扩展模块。但是,为了避免加载每个程序集,我更喜欢防御性方法: 使用 Assembly.Re
我正在移植一个非常大的代码库,我在处理旧代码时遇到了更多困难。 例如,这会导致编译器错误: inline CP_M_ReferenceCounted * FrAssignRef(CP_M_Refere
[关于此主题还有其他类似的问题,但是它们都没有回答我在这里提出的问题,即AFAICT。 (即,我已经阅读了所有答案,解释了为什么特定构造无法与发问者尝试进行的操作,在某些情况下,它们提供了获得所需结果
嗨 我想为需要全屏运行的网络艺术应用程序构建一个控制面板,因此所有控制颜色和速度值等内容的面板都必须位于不同的窗口中。 我的想法是建立一个数据库来存储所有这些值,当我在控制面板窗口中进行更改时,应用程
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
假设我想实现一个分布式数据库(每个节点都是其他节点的副本);我听说 cdb 能够轻松地在两个节点之间进行同步,并且至少支持某种形式的冲突解决。 不幸的是我不知道 couchdb 因此我不得不问:节点“
我是一名优秀的程序员,十分优秀!