问题
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
}]
... which I pull from the web service and store in a field called "My JSON".
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
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?
Thanks,
Steve
回答1:
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
来源:https://stackoverflow.com/questions/16607199/square-brackets-with-mergjson-in-livecode-what-am-i-doing-wrong