如何从对象中提取内容? [英] How do you extract content out of object?
问题描述
我尝试通过AJAX远程获取数据。
I am trying to get data remotely via AJAX.
当我通过 document.write(obj)
,我在我的浏览器上得到这个:
When I look at the data via document.write(obj)
, I get this on my browser:
[object Object]
这是什么意思?这是一个数组或数组数组?
What does this mean? Is this an array or array of arrays?
我对JavaScript非常新,我需要看看这个obj,并得到文本。我真的很感激任何洞察力。
I am very new to JavaScript, I need to look inside this obj and get the text out of. I would really appreciate any insight.
我使用 console.log(data)
输出。我需要从此输出中删除 \
。我试过:
I have used console.log(data)
, and I am seeing this output. I need to get rid of \
from this output. I tried:
data.strReplace(/\\/g/,'');
无效。
数据如下所示:
["{\"name\":\"ServerA\",\"data\":[[1406284185600,0.092],[1406285985600,0.092]]}]
这里是我看到的屏幕截图:
Here's a screenshot of what I'm seeing:
这是控制台输出:
This is the console output:
推荐答案
不要使用 document.write / code>进行调试。
Don't use document.write()
for debugging.
使用 console.log()
并检查浏览器的开发人员工具。
Use console.log()
and check your browser's developer tools.
[object Object]
是将对象显示为字符串的方式:你可以看到用 alert()
,这是人们错误地用来调试的另一件事。
[object Object]
is the way that an object is displayed as a string: you're seeing that because you're displaying the data as HTML. You'd also see that with alert()
, which is another thing that people mistakenly use for debugging.
控制台会显示实际的数据,如下所示:
The console will show you the actual piece of data, like this:
:这里有一些错误。
- 反斜杠有一个原因:它们逃避形成字符串的引号。你想删除它们的唯一原因是将字符串转换为一个对象(这是我假设你想做的。
-
应该是
- The backslashes are there for a reason: they escape the quotation marks that form the string. The only reason you'd want to remove them is to convert the string to an object (which is what I assume you're trying to do.
You've removed a quotation mark in-between the last closing curly brace and the last closing bracket. It should be
["{\"name\":\"ServerA\",\"data\":[[1406284185600,0.092],[1406285985600,0.092]]"}
这是你有:
["{\"name\":\"ServerA\",\"data\":[[1406284185600,0.092],[1406285985600,0.092]]}
(查看语法高亮的区别。)
(Look at the difference in syntax highlighting.)
您的正则表达式有错字:it应为 / \\ / g
,而不是 / \\ / g /
。
Your regular expression has a typo: it should be /\\/g
, not /\\/g/
.
,它工作正常。
Once you fix all of that, it works fine.
这篇关于如何从对象中提取内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!