热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

遇到JSON和Javascript问题-HavingtroublewithJSONandJavascript

Iamtryingtomakeasimplesamplepreviewerformyclientproject.Thelistofsamplesarestored

I am trying to make a simple sample previewer for my client project. The list of samples are stored in a JSON file. but the trouble is that I am not too familiar with using JSON, or programming itself, to be honest. I have written a short test code to see if this JSON file works well, but although I can access sampleData variable from firebug console, the document.write(sampleData.length); line seems to be unable to access sampleData for some reason. The error shows:

我正在尝试为我的客户端项目制作一个简单的示例预览器。样本列表存储在JSON文件中。但问题是,我不太熟悉使用JSON或编程本身,说实话。我编写了一个简短的测试代码来查看这个JSON文件是否运行良好,但是我可以从firebug控制台访问sampleData变量,document.write(sampleData.length);由于某种原因,行似乎无法访问sampleData。错误显示:

TypeError: sampleData is undefined

TypeError:sampleData未定义

I suspect it has to do with variable scopes, but I am not sure.

我怀疑它与变量范围有关,但我不确定。



What should I do to make the sampleData variable to work with the rest of the code?
I'm sorry I cannot disclose the contents of the json file. It contains an array of objects containing information of the products. it looks like

我该怎么做才能使sampleData变量与其余代码一起使用?对不起,我不能透露json文件的内容。它包含一系列包含产品信息的对象。看起来像

[
    {
    "aaa" : 000000;
    },
    {
    "bbb" : 111111;
    },
    {
    "ccc" : 222222;
    }
]

It's a quite generic form of JSON file, as far as I can tell.

据我所知,它是一种非常通用的JSON文件形式。

5 个解决方案

#1


Your code's fine, but $.getJSON just starts the process of getting the JSON file, and then allows your code to continue, calling document.write. The callback to $.getJSON is called later, after your document.write call. Instead, trigger any subsequent code you want to trigger from within the callback.

你的代码很好,但$ .getJSON只是启动获取JSON文件的过程,然后允许你的代码继续,调用document.write。在您的document.write调用之后,稍后将调用$ .getJSON的回调。而是在回调中触发您想要触发的任何后续代码。

You can't usefully use document.write in this situation. You can use the DOM, though, and jQuery's various DOM wrapper functions. For instance:

在这种情况下,您无法有效地使用document.write。但是,您可以使用DOM和jQuery的各种DOM包装函数。例如:

$.getJSON('res/Samples.json',function(data){
    $("

").html(data.length).appendTo(document.body); });

#2


It's due to the async response from $.getJSON.

这是由于$ .getJSON的异步响应。

var sampleData;
$.getJSON('res/Samples.json', function(data) {
  sampleData = data; // This is occurring after the document.write
});
document.write(sampleData.length);

This is essentially the same as:

这基本上与以下相同:

var sampleData;
setTimeout(function() {
  sampleData = 'Some data'; // This is occurring after the document.write
}, 100);
document.write(sampleData.length);

You could move the document.write to the response handler but as noted below it does have its drawbacks:

您可以将document.write移动到响应处理程序,但如下所述它确实有它的缺点:

var sampleData;
$.getJSON('res/Samples.json', function(data) {
  document.write(sampleData.length); // This would replace the page contents
});

#3


It happens asynchronously, so you need to call document.write in the async call itself:

它是异步发生的,因此您需要在异步调用本身中调用document.write:

var sampleData;
$.getJSON('res/Samples.json',function(data){
    sampleData = data;
    document.write(sampleData.length);
});

Also, if you're using document.write in your production code to write to the page, I would suggest against it. If you used document.write in the above example just for debugging purposes (to figure out why it wasn't working), I would suggest using console.log instead. Much easier.

此外,如果您在生产代码中使用document.write写入页面,我建议不要这样做。如果你在上面的例子中使用document.write只是为了调试目的(弄清楚它为什么不工作),我建议改用console.log。更容易。

#4


$.getJSON is an async function and when you do document.write(sampleData.length);, sampleData is not populated yet.

$ .getJSON是一个异步函数,当你执行document.write(sampleData.length);时,还没有填充sampleData。

You must to do:

你必须这样做:

$.getJSON('res/Samples.json',function(data){
   sampleData=data;
   document.write(sampleData.length);
});

#5


Well is reason is you call the sampleData.length before the ajax call return the values you need to put the document.write(sampleData.length); inside the ajax function

好吧,原因是你在ajax调用之前调用sampleData.length返回你需要放置document.write(sampleData.length);在ajax函数里面




推荐阅读
  • 本文介绍了在Vue项目中如何结合Element UI解决连续上传多张图片及图片编辑的问题。作者强调了在编码前要明确需求和所需要的结果,并详细描述了自己的代码实现过程。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • PHP中的单例模式与静态变量的区别及使用方法
    本文介绍了PHP中的单例模式与静态变量的区别及使用方法。在PHP中,静态变量的存活周期仅仅是每次PHP的会话周期,与Java、C++不同。静态变量在PHP中的作用域仅限于当前文件内,在函数或类中可以传递变量。本文还通过示例代码解释了静态变量在函数和类中的使用方法,并说明了静态变量的生命周期与结构体的生命周期相关联。同时,本文还介绍了静态变量在类中的使用方法,并通过示例代码展示了如何在类中使用静态变量。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 标题: ... [详细]
  • 本文介绍了在使用MSXML解析XML文件时出现DTD禁用问题的解决方案。通过代码示例和错误信息获取方法,解释了默认情况下DTD是禁用的,以及如何启用DTD的方法。此外,还提到了网上关于该问题的信息相对较少,因此本文提供了解决方案以供参考。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 如何提高PHP编程技能及推荐高级教程
    本文介绍了如何提高PHP编程技能的方法,推荐了一些高级教程。学习任何一种编程语言都需要长期的坚持和不懈的努力,本文提醒读者要有足够的耐心和时间投入。通过实践操作学习,可以更好地理解和掌握PHP语言的特异性,特别是单引号和双引号的用法。同时,本文也指出了只走马观花看整体而不深入学习的学习方式无法真正掌握这门语言,建议读者要从整体来考虑局部,培养大局观。最后,本文提醒读者完成一个像模像样的网站需要付出更多的努力和实践。 ... [详细]
  • Week04面向对象设计与继承学习总结及作业要求
    本文总结了Week04面向对象设计与继承的重要知识点,包括对象、类、封装性、静态属性、静态方法、重载、继承和多态等。同时,还介绍了私有构造函数在类外部无法被调用、static不能访问非静态属性以及该类实例可以共享类里的static属性等内容。此外,还提到了作业要求,包括讲述一个在网上商城购物或在班级博客进行学习的故事,并使用Markdown的加粗标记和语句块标记标注关键名词和动词。最后,还提到了参考资料中关于UML类图如何绘制的范例。 ... [详细]
  • VueCLI多页分目录打包的步骤记录
    本文介绍了使用VueCLI进行多页分目录打包的步骤,包括页面目录结构、安装依赖、获取Vue CLI需要的多页对象等内容。同时还提供了自定义不同模块页面标题的方法。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
author-avatar
泥泥的春天_565_576
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有