• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Dynamic search for json properties
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dynamic search for json properties


  • Subject: Re: Dynamic search for json properties
  • From: Jörgen Stahle <email@hidden>
  • Date: Fri, 18 Nov 2016 12:02:04 +0000
  • Thread-topic: Dynamic search for json properties

I forgot to respond! 
The ASObjc-code works fine, and is exactly what I wished for! I made a script library out of it. Perfect.
Thanks again, Shane. A lot!!!
Jörgen

16 nov. 2016 kl. 00:17 skrev Shane Stanley <email@hidden>:

On 16 Nov. 2016, at 8:24 am, Jörgen Stahle <email@hidden> wrote:

Then I just have to call JSA from Applescript. I Guess it is as easy as calling another applescript.

I don't believe it's quite that simple. Last I looked, the only way of loading a JXA lib and calling one of its functions that I could find was anything but obvious. 

Any thoughts about that, anyone? 

You could always do something similar in AppleScript. Instead of converting the JSON to a dictionary and then an AS record, leave it as a Cocoa dictionary. Then you can use valueForKeyPath: in a similar way:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

on convertJSONToDictionary:jsonString
set aString to current application's NSString's stringWithString:jsonString
set theData to aString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set {theDict, theError} to current application's NSJSONSerialization's JSONObjectWithData:theData options:0 |error|:(reference)
if theDict is missing value then error (theError's localizedDescription() as text) number -10000
return theDict
end convertJSONToDictionary:

set theJSON to "{\"timeblocksDefs\": {
    \"normal\": {
        \"morning\": \"06-09\",
        \"day\": \"09-18\",
        \"primetime\": \"18-22\",
        \"nighttime\": \"22-06\"
    },
    \"lateprime\": {
        \"morning\": \"06-09\",
        \"day\": \"09-19\",
        \"primetime\": \"19-23\",
        \"nighttime\": \"23-06\"
    }
}}
"
set theDict to my convertJSONToDictionary:theJSON
set theResult to (theDict's valueForKeyPath:"timeblocksDefs.lateprime")
-- convert to AS equivalent
set theResult to item 1 of ((current application's NSArray's arrayWithObject:theResult) as list)



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Re: Dynamic search for json properties (From: Simon Forster <email@hidden>)
 >Re: Dynamic search for json properties (From: Jörgen Stahle <email@hidden>)
 >Re: Dynamic search for json properties (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Sal
  • Next by Date: Re: Sal
  • Previous by thread: Re: Dynamic search for json properties
  • Next by thread: Re: Dynamic search for json properties
  • Index(es):
    • Date
    • Thread