javascript 库将字符串解释为 JSON 对象。有什么想法吗?
下面是查询结果: ["Language1","anotherLanguage","yetanotherlangugae"]
Javascriptmvc 的模型类 findAll 方法返回(通过成功回调)一个 javascript 对象数组。该框架负责将服务器返回的文本转换为 JavaScript 对象数组。如果查询结果如上,则数组中的每个字符串都被视为一个关联数组,键为字符的索引,值为字符。结果如下所示。
调用 JSON.stringify 的结果如下所示。
[{"0":"L","1":"a","2":"n","3":"g","4":"u","5":"a","6":"g","7":"e","8":"1"},{"0":"a","1":"n","2":"o","3":"t","4":"h","5":"e","6":"r","7":"L","8":"a","9":"n","10":"g","11":"u","12":"a","13":"g","14":"e"},{"0":"y","1":"e","2":"t","3":"a","4":"n","5":"o","6":"t","7":"h","8":"e","9":"r","10":"l","11":"a","12":"n","13":"g","14":"u","15":"g","16":"a","17":"e"}]
查询由 javascriptmvc 模型完成,上面显示的返回结果传递到成功回调中。查询结果将转换为三个 JSON 对象的数组。这是怎么回事?
这不是调用 JSON.parse 的问题。当响应通过成功回调传递给我时,它已经被解析为 json 对象。
结果应该是一个 JSON 对象数组,如果以自然方式序列化为字符串,它看起来就像来自服务器的原始响应。这里的情况并非如此,因此也是问题所在。
这是执行实际 findAll 查询的代码片段:
$.ajax({
url: '/language',
type: 'get',
dataType: 'json',
data: params,
success: this.callback(['wrapMany',success]),
error: error
});
Here's the query result: ["Language1","anotherLanguage","yetanotherlangugae"]
Javascriptmvc's model classes findAll method returns (via success callback) an array of javascript objects. The framework takes care of converting the text returned by the server to an array of javascript objects. If the query result is as above, each string in the array is treated as a an associative array with the key the index of the character and the value the character. The result is shown below.
The results of a call to JSON.stringify are shown below.
[{"0":"L","1":"a","2":"n","3":"g","4":"u","5":"a","6":"g","7":"e","8":"1"},{"0":"a","1":"n","2":"o","3":"t","4":"h","5":"e","6":"r","7":"L","8":"a","9":"n","10":"g","11":"u","12":"a","13":"g","14":"e"},{"0":"y","1":"e","2":"t","3":"a","4":"n","5":"o","6":"t","7":"h","8":"e","9":"r","10":"l","11":"a","12":"n","13":"g","14":"u","15":"g","16":"a","17":"e"}]
The query is done by a javascriptmvc model and the result returned shown above passed into the success callback. The query result is transformed into an array of three JSON objects. What's going on here?
This is not an issue of calling JSON.parse. The response has already been parsed into a json object by the time it has been handed to me via the success callback.
The result should be an array of JSON objects that, if serialized into a string in a natural way, looks just like the original response from the server. That's not the case here, and hence the problem.
Here's the snippet that does the actual findAll query:
$.ajax({
url: '/language',
type: 'get',
dataType: 'json',
data: params,
success: this.callback(['wrapMany',success]),
error: error
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到了答案。
当您使用 javascriptmvc 生成模型时,它会为 findAll 方法生成类似以下内容的内容:
注意注释掉的行。它对响应调用wrapMany。预期响应是 JSON 对象数组,而不是字符串。如果响应是字符串数组,则每个字符串都会转换为 JSON 对象,从而得到您在问题中看到的结果。
因此,服务器不应返回 [
"lan1", "lan2"]
,而是应返回类似[{"language" : "lan1"}, {"language" : "lan2"} 的内容]
Found the answer.
When you use javascriptmvc to generate a model, it generates something like the following for the findAll method:
Notice the commented out line. It calls wrapMany on the response. The expected response is an array of JSON objects, not strings. If the response is an array of strings, each string is converted into a JSON object, and hence the result you see in the question.
So instead of returning [
"lan1", "lan2"]
the server should respond with something like[{"language" : "lan1"}, {"language" : "lan2"}]
我认为您正在寻找
json.parse(json_string)
。json.stringify()
正在做它应该做的事情 - 将 Javascript 视为字符数组的内容转换为 JSON 样式的字符数组。I think you're looking for
json.parse(json_string)
.json.stringify()
is doing what it's supposed to - converting what Javascript sees as an array of characters into a JSON-style array of characters.也许如果您的查询返回:
您会得到您想要的。
maybe if your query returned:
you would get what you want.