如何使用 JQuery 读取 JSON 结果中的 Fields:{} 内容?
我对整个 JSON 和 JQuery 的事情很陌生,我正在尝试读取来自 Delphi datasnap 的 JSON 结构,例如:
{"result":[[{"type":"VOMesas.TMesas","id":1,"fields":{ "FUsers":1,"FEnclosing":0,"FClientName":"","FCode":100,"FStatus":1,"FTotalValue":128.25}}]]}
如何使用 JQuery 读取它,更具体地说是 Fields:{...} content ?
编辑:
这是我想做的功能
function getContent(order) {
$.getJSON("query.json",
function(data) {
$.each(data.result, function(i, item) {
var grid = '<table border="1">';
for (var i=0; i < item.length; i++){
CAMPO = item[i];
...
I'm fresh to this whole JSON and JQuery thing, and I'm trying to read a JSON structure coming from Delphi datasnap, e.g :
{"result":[[{"type":"VOMesas.TMesas","id":1,"fields":{ "FUsers":1,"FEnclosing":0,"FClientName":"","FCode":100,"FStatus":1,"FTotalValue":128.25}}]]}
How can I read it with JQuery, more especifically the Fields:{...} content ?
EDIT :
here is the function im trying to do
function getContent(order) {
$.getJSON("query.json",
function(data) {
$.each(data.result, function(i, item) {
var grid = '<table border="1">';
for (var i=0; i < item.length; i++){
CAMPO = item[i];
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您通过 jQuery.ajax 或类似方式加载数据,并且它以正确的 MIME 类型返回(或者您告诉 jQuery.ajax 您要返回的内容)是 JSON),那么您在
success
回调中收到的将是一个反序列化的对象(不再是 JSON,而是 JSON 描述的对象)。在这种情况下,您只需访问对象的属性,例如:data
是指向该对象的变量,它有一个result
属性,该属性是一个只有一个的数组条目(所以,条目[0]
),它本身是另一个只有一个条目的数组(所以,又是条目[0]
),它是一个具有属性的对象称为字段
。如图所示:如果您以其他方式检索数据并且它仍然是一个字符串,您可以使用
jQuery.parseJSON
:...然后执行上述操作来访问
字段
。If you're loading the data via
jQuery.ajax
or similar and it's being returned with the correct MIME type (or you telljQuery.ajax
that what you're getting back is JSON), then what you receive in thesuccess
callback will be a deserialized object (no longer JSON, but the objects that the JSON described). That being the case, you just access the properties of the object, e.g.:data
being the variable pointing to the object, which has aresult
property that's an array with only one entry (so, entry[0]
), which is itself another array with exactly one entry (so, entry[0]
again), which is an object with a property calledfields
. Pictorially:If you're retrieving the data some other way and it's still a string, you can deserialize it using
jQuery.parseJSON
:...and then do the above to access
fields
.如果您有 JSON 字符串,只需使用 JSON.parse 将其转换为 Javascript 对象即可。
但是,请注意,您粘贴到问题中的 JSON 无效:
If you've got a string of JSON, simply use
JSON.parse
to turn it into a Javascript object.By aware, however, that the JSON you've pasted into your question is invalid:
首先:假设您将 JSON 对象分配给变量“myobject”。然后你可以做
First: Let's assume you assigned your JSON object to a variable "myobject". Then you can do
希望这个代码示例能够帮助您理解 jQuery/JSON 的东西。
我已将示例 JSON 对象作为数组。然后通过读取 JSON 的键/值对来填充小 HTML。
工作示例: http://jsfiddle.net/ylokesh/WC84k/
Hope this code example will help you to understand the jQuery/JSON thing.
I have taken the sample JSON object as an array. Then populating small HTML by reading key/value pair of JSON.
working example: http://jsfiddle.net/ylokesh/WC84k/