JavaScript 对象迭代
由于我正在进行 AJAX 调用,我无法访问从服务器端脚本以 JSON 形式接收的对象中的某些属性。我的 JSON 数据可以采用两种形式之一,具体取决于对象本身的数据。
表格 1:
"MY_OBJ" : { "FILE" : "myfile.txt",
"PARAMS" : { "PARAM" : { "KEY" : "mykey",
"VALUE" : "myvalue"
} }
}
表格 2:
"MY_OBJ" : { "FILE" : "myfile.txt",
"PARAMS" : { "PARAM" : [ { "KEY" : "mykeyone",
"VALUE" : "myvalueone"
},
{ "KEY" : "mykeytwo",
"VALUE" : "myvaluetwo"
}
] }
}
这就是我当前尝试解析要在浏览器中显示的数据的方式:
function(v) {
var myFormattedData = v.FILE;
if (v.PARAMS !== undefined && v.PARAMS.PARAM !== undefined && v.PARAMS.PARAM.KEY !== undefined && v.PARAMS.PARAM.VALUE !== undefined) {
myFormattedData += '<br />' + v.PARAMS.PARAM.KEY + ' : ' + v.PARAMS.PARAM.VALUE;
} }
当我的数据位于表格 1 中时,此方法工作正常。在这种情况下,我将在浏览器中获得输出,例如
myfile.txt
mykey : myvalue
...。 ..这正是我想要的。
但是,当数据采用 Form 2 时,我得到的只是浏览器中显示的文件名,如下所示......
myfile.txt
但我想要得到的是......
myfile.txt
mykeyone : myvalueone
mykeytwo : myvaluetwo
我需要能够处理获取两种形式的数据。更糟糕的是,我可能还应该计划在未来某个时候接收不仅仅是一两个键值对的可能性。我已经为此苦苦挣扎了很长时间。非常感谢任何帮助!
谢谢你!
I am having trouble accessing certain properties in an object that I receive as JSON from a server-side script, as the result of an AJAX call that I am making. My JSON data can come in one of two forms, depending on the data in the object itself.
Form 1:
"MY_OBJ" : { "FILE" : "myfile.txt",
"PARAMS" : { "PARAM" : { "KEY" : "mykey",
"VALUE" : "myvalue"
} }
}
Form 2:
"MY_OBJ" : { "FILE" : "myfile.txt",
"PARAMS" : { "PARAM" : [ { "KEY" : "mykeyone",
"VALUE" : "myvalueone"
},
{ "KEY" : "mykeytwo",
"VALUE" : "myvaluetwo"
}
] }
}
This is how I am currently trying to parse the data to display in the browser:
function(v) {
var myFormattedData = v.FILE;
if (v.PARAMS !== undefined && v.PARAMS.PARAM !== undefined && v.PARAMS.PARAM.KEY !== undefined && v.PARAMS.PARAM.VALUE !== undefined) {
myFormattedData += '<br />' + v.PARAMS.PARAM.KEY + ' : ' + v.PARAMS.PARAM.VALUE;
} }
This method works fine when my data is in Form 1. In that situation I will get output in the browser like...
myfile.txt
mykey : myvalue
...which is exactly what I want.
However, when the data is in Form 2 all I get is the file name displayed in the browser like this...
myfile.txt
... but what I am trying to get is something like...
myfile.txt
mykeyone : myvalueone
mykeytwo : myvaluetwo
I need to be able to handle getting the data in both forms. What's worse is that I probably also should plan for the possibility of receiving more than just one or two key-value pairs at some point in the future. I have been struggling with this for a long time. Any help is much appreciated!
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了简化处理,请检查
PARAM
对象的类型。如果它是一个数组,那么您将得到类型 2 的响应。否则,它是类型 1 的响应。通过将PARAM
包装在数组内,将类型 1 转换为类型 2 进一步简化,这样您就可以将无论服务器如何发送它,所有内容在客户端都作为数组。To simplify processing, check the type of the
PARAM
object. If it is an array, then you have a response of type 2. Otherwise it's a response of type 1. Further simplify by converting type 1 to type 2 by wrappingPARAM
inside an array, so you can treat everything as an array on the client side regardless of how the server sends it.尝试对第二种形式执行类似的操作:
Try doing something like this for the second form:
在 form1 中,
v.PARAMS.PARAM.KEY
将为非空。在表单 2 中,它将为 null。
在这种情况下,您需要将键和值引用为
v.PARAMS.PARAM[0].KEY
和v.PARAMS.PARAM[0].VALUE
,并且v.PARAMS.PARAM[1].KEY
In form1,
v.PARAMS.PARAM.KEY
will be non-null.In form 2, it will be null.
In this case, you need to reference your keys and values as
v.PARAMS.PARAM[0].KEY
andv.PARAMS.PARAM[0].VALUE
, andv.PARAMS.PARAM[1].KEY
在第二个版本中,
v.PARAMS.PARAM
是一个数组。您应该检查它是否确实是一个数组,然后迭代该数组。以下代码仅适用于表单 2。如果任一表单都可能从服务器返回,则您需要将您的函数与我所展示的函数混合。In the second version,
v.PARAMS.PARAM
is an Array. You should check that it actually is an Array, then iterate that Array. The following code would only work for form 2. If it is possible that either form should return from the server, you'll need to blend your function with what I show.