使用 YUI 解析 JSON,数据包中的 {} 周围带有 []
我对 JSON、javascript、YUI 很陌生,正在尝试完成作业。我有一个格式为
[{"id":"1234", "name":"some description","description":"url":"www.sd.com"}, {same format如前一个}]
我尝试使用 YUI 来传递 jsonString,如下所示:
var jsonString = '{"id":"1234", "name":"some description","description":"url":"www.sd.com"}';
var messages = [];
messages = YAHOO.lang.JSON.parse(jsonString);
然后在 messages[] 中打印结果。在 jsonString 示例中,我得到了输出。当尝试从教授的网络服务器上执行此操作时,我解析失败。我认为这是因为他的数据包被 [] 包围,而我的数据包不在我的示例中。我尝试执行
YAHOO.lang.JSON.parse(professorResponse[0]);
并且也返回了错误。我想知道在这种情况下最好的格式/实践是什么,如何处理从网络服务器传回的数据,如何格式化数据,以便我可以解析它。我在这方面的经验为零,希望有一个好的开始。谢谢。
编辑:
To parse the web server's response, I'm doing this:
function sendRequest() {
var url = "class website&response=JSON";
var callback = {success:handleResponse, failure:handleFailure, timeout:5000};
var transaction = YAHOO.util.Connect.asyncRequest("GET", url, callback, null);
}
// this gets called when my handleResponse methods looks if it's JSON vs XML and sees that the server's response is JSON
function parseJSONResponse(response) {
var messages = [];
try {
messages = YAHOO.lang.JSON.parse(response);
}
catch (e) {
alert("JSON parse failed");
return;
}
}
当我尝试解析响应时,我仍然遇到 JSON Parse 失败的情况。
I'm pretty new to JSON, javascript, YUI and trying to compelte a homework assignment. I have a JSON packet of the form
[{"id":"1234", "name":"some description","description":"url":"www.sd.com"}, {same format as previous one}]
I tried an example with YUI to pase a jsonString like this:
var jsonString = '{"id":"1234", "name":"some description","description":"url":"www.sd.com"}';
var messages = [];
messages = YAHOO.lang.JSON.parse(jsonString);
then I print out the result in messages[]. In the jsonString example, I get my output. When trying to do it from the professor's web server, I get a failure on parsing. I'm assming it's from the way his packet is surrounded by a [] and mine is not in my example. I tried doing
YAHOO.lang.JSON.parse(professorResponse[0]);
and that also returned an error. I was wondering what the best format/practices in this scenario on what to do with something passed back from a web server in terms of how to format the data so I can parse it. I have zero experience in this area and want to get off to a good start. Thanks.
Edit:
To parse the web server's response, I'm doing this:
function sendRequest() {
var url = "class website&response=JSON";
var callback = {success:handleResponse, failure:handleFailure, timeout:5000};
var transaction = YAHOO.util.Connect.asyncRequest("GET", url, callback, null);
}
// this gets called when my handleResponse methods looks if it's JSON vs XML and sees that the server's response is JSON
function parseJSONResponse(response) {
var messages = [];
try {
messages = YAHOO.lang.JSON.parse(response);
}
catch (e) {
alert("JSON parse failed");
return;
}
}
I continue to get JSON Parse failed when I try to parse the response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是有效的 JSON,每个键都需要一个值。您缺少“描述”的值
它应该看起来像这样:
This isn't valid JSON, every key needs a value. You're missing a value for "description"
It should look something like this:
您的示例和教授的示例都是无效的 JSON,无法解析,因为两者都包含没有值的“描述”属性:
应该是:(
或者只是删除
“description”:
)我的答案的其余部分假设在继续之前可以修复上述问题...
您没有解释如何获取教授的 JSON,或者如何生成输出,但一般意义上 JSON 是一个字符串 em> 对象或数组的表示,然后解析创建实际的对象或数组。您的示例是对象的表示。您教授的示例是对象数组的表示。
你出错的地方是你似乎试图将
professorResponse
视为一个数组,并使用professorResponse[0]
before 访问其元素 0你已经解析了它,但如果它是 JSON,那么它是一个表示数组而不是实际数组的字符串,因此你需要首先解析它:注意:在你的示例中,你初始化
messages
来引用空数组,但你立即将其分配为等于YAHOO.lang.JSON.parse(jsonString)
的返回值,在这种情况下,它不是一个数组(因为您的 jsonString 代表一个不是数组的对象)一个数组) - 你原来的空数组被丢弃。如果 Web 服务器返回有效的 JSON,您无需对其进行格式化即可解析它 - 它已经是可以使用
JSON.parse()
进行解析的格式的字符串。Both your example and the professor's are invalid JSON that can't be parsed because both include a "description" property that doesn't have a value:
Should be:
(Or just remove
"description":
)The rest of my answer assumes the above can be fixed before continuing...
You don't explain how you are getting your professor's JSON, or how you generate your output, but in a general sense JSON is a string representation of an object or array, that you then parse to create an actual object or array. Your example is a representation of an object. Your professor's example is a representation of an array of objects.
Where you're going wrong is you seem to be trying to treat
professorResponse
as an array and access its element 0 withprofessorResponse[0]
before you've parsed it, but if it is JSON then it is a string representing an array not an actual array so you need to parse it first:Note: in your example you initialise
messages
to refer to an empty array, but then you immediately assign it equal to the return fromYAHOO.lang.JSON.parse(jsonString)
which in this case will not be an array (because your jsonString represents an object that is not an array) - your original empty array is thrown away.If the web server is returning valid JSON you don't need to format it in order parse it - it will already be a string in a format that can be parsed with
JSON.parse()
.那么 [] 是一个数组,而 {} 是一个对象。
因此,如果您有以下内容:
jsonString.id
将返回 1234。因此,在上面的示例中:
您将使用:
someData[0].id
来获取 ID第一个对象。Well [] is an array and {} an object.
So if you had the following:
jsonString.id
would return 1234.So in this example from above:
You would use:
someData[0].id
to get the ID of the first object.