LiveCode 中带有 mergJSON 的方括号 - 我做错了什么? [英] Square brackets with mergJSON in LiveCode - what am I doing wrong?
问题描述
我要连接的 Web 服务返回其 JSON 以响应查询,因此:
The web service I'm connecting to returns its JSON in response to a query, thus:
[{
"dob":"25/12/68",
"firstname":"Mary",
"lastmoddate":1368519205101,
"lastname":"Smith",
"resident_id":"712d9b726603426ca36f9c77fa644ae9",
"createddate":1368519205101
}]
...我从网络服务中提取并存储在名为我的 JSON"的字段中.
... which I pull from the web service and store in a field called "My JSON".
您会注意到开头和结尾都有方括号,我认为这对 JSON 数组有效?我在 LC 中有以下脚本,目前它只是从我的数组中检索名字"并将其放入 tMyVar 以进行显示:
As you'll notice there are square brackets at the beginning and end which I believe is valid for JSON arrays? I have the following script in LC which at the moment just retrieves the "firstname" from my array and puts it into the tMyVar for display:
function JSONToArray pJSON
local tArray,tKeys
repeat for each line tKey in mergJSONDecode(pJSON,"tArray")
put JSONToArray(tArray[tKey]) into tArray[tKey]
end repeat
return tArray
end JSONToArray
on mouseUp
put field "My JSON" into pJSON
put JSONToArray(pJSON) into tArray
put tArray["firstname"] into tMyVar
answer tMyVar
end mouseUp
这很奇怪,但是当我通过 mergJSON WITH 运行 Web 服务 JSON 时,方括号 tMyVar 变量为空,但是如果我从开头删除方括号 &结束变量填充名字"的内容就好了.
It's odd but when I run the web service JSON through mergJSON WITH the square brackets the tMyVar variable is empty, but if I remove the square brackets from beginning & end the variable gets populated with the contents of "firstname" just fine.
有人可以建议我做错了什么吗?
Can anyone suggest what I'm doing wrong please?
谢谢,
史蒂夫
推荐答案
当它返回一个数组内的对象时,JSONToArray 将创建一个多维数组.将 mouseUp 处理程序更改为:
As it's returning an object inside an array then JSONToArray will create a multi-dimensional array. Change your mouseUp handler to:
on mouseUp
put field "My JSON" into pJSON
put JSONToArray(pJSON) into tArray
put tArray[1]["firstname"] into tMyVar
answer tMyVar
end mouseUp
这篇关于LiveCode 中带有 mergJSON 的方括号 - 我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!