我有这个JSON文件,需要从中提取数据:
{
"data1": "Hello",
"data2":[
{
"value1": "1092",
"value2": "1242",
"value3": "4234",
}
],
"data3": "Bye"
}我知道如何通过这段代码从"data1"和"data3"获取值
set mJson to do shell script "curl -s 'https://apiURL...'"
set AppleScript's text item delimiters to {","}
set keyValueList to (every text item in mJson) as list
set AppleScript's text item delimiters to ""
set theKeyValuePair to item 3 of keyValueList
set AppleScript's text item delimiters to {": "}
set theKeyValueBufferList to (every text item in theKeyValuePair) as list
set AppleScript's text item delimiters to ""
set theValue to item 2 of theKeyValueBufferList
set value to do shell script "sed s/[^0-9.,]//g <<< " & theValue
return "$" & text 1 thru 7 of value但是如何获取"data2" > "value1"中的值,因为它位于JSON文件的“子类别”中?
有人能帮帮我吗?
发布于 2019-03-20 20:11:55
用普通的AppleScript解析JSON是非常烦人的。
您可以使用JSON Helper之类的辅助应用程序,也可以在基础框架的帮助下执行此操作
set json to "{\"data1\": \"Hello\",\"data2\":[{\"value1\": \"1092\",\"value2\": \"1242\",\"value3\": \"4234\",
}],\"data3\": \"Bye\"}"
set foundationString to current application's NSString's stringWithString:json
set jsonData to foundationString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set {dataDictionary, jsonError} to current application's NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(reference)
if jsonError is missing value then
set data1 to (dataDictionary's objectForKey:"data1") as text
set data2 to (dataDictionary's objectForKey:"data2")'s objectAtIndex:0
set value1 to (data2's objectForKey:"value1") as text
end ifhttps://stackoverflow.com/questions/55260107
复制相似问题