NSURLConnection Response

hi i am trying to get json data from an NSURLConnection Response but after parsing, its integer values of customer phoneNumbers (6.7221091E+8) seem to overload , i have tried various methods, (NSString’s alloc()'s initWithData, NSJSONSerialization’s JSONObjectWithData) which i do end up with a record but with the overloaded results of the integer values.

when i return the result as text

resStr to current application’s NSString’s alloc()'s initWithData:bRes encoding:((current application’s NSUTF8StringEncoding))
set resStr to resStr as text
return resStr

the number is correct, but after converted to record its phoneNumber value is overloaded

I think i need to parse with long ints or All values as text.
Anyone know a solution to this? thanks in advance
thanks John

When you call JSONObjectWithData:options:error:, what is the Cocoa class of the object returned?

Hi thanks for you’re interest,

the class is «class ocid» id «data optr00000000605FB5ACFF7F0000»
thanks

ok so i did a bit more playing around and i discovered a bit more

set jsonText to “{"customerPhone":630408268}”
set jsonString to current application’s NSString’s stringWithString:jsonText
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
set theJSON to NSJSONSerialization’s JSONObjectWithData:jsonData options:(current application’s NSJSONReadingAllowFragments) |error|:(missing value)

theJSON as record → {customerPhone:6.30408268E+8}

(theJSON’s valueForKey:(“customerPhone”)) as text → “630408268”

so i can get the value from the NSJSONData so it is the record that causing a problem
it seems any integer over 8 digits long, at least i know the data holds the value correctly

It’s not the record which causes the issue, it’s the way AppleScript displays large integer values.

In terms of Objective-C the dictionary value is an NSNumber object which has a stringValue property:

set phoneNumber to (theJSON's objectForKey:("customerPhone"))'s stringValue() as text

Note:

Please don’t use valueForKey. It’s an API related to Key Value Coding which has a slightly different behavior. Use always objectForKey unless you really need KVC.

And the option allowFragments is pointless if the expected object is a dictionary or array.

Hello

I’m trying to understand this thread’s contents but when I run this script:

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

set jsonText to "{\"customerPhone\":630408268}"
set jsonString to current application's NSString's stringWithString:jsonText
set jsonData to jsonString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set theJSON to NSJSONSerialization's JSONObjectWithData:jsonData options:(current application's NSJSONReadingAllowFragments) |error|:(missing value)


theJSON as record --> {customerPhone:6.30408268E+8}

I get :

error “La variable NSJSONSerialization n’est pas définie.” number -2753 from “NSJSONSerialization”

Am’I failing to call a framework ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 13 janvier 2019 19:28:15

Yvan,

all Cocoa classes have to be addressed with tell current application, there is one missing the in the NSJSONSerialization line

set theJSON to current application's NSJSONSerialization's JSONObjectWithData:jsonData options:(current application's NSJSONReadingAllowFragments) |error|:(missing value)

Some people (me too) declare a property at the beginning of the script

property |⌘| : a reference to current application

so you can shorten the code

set theJSON to |⌘|'s NSJSONSerialization's JSONObjectWithData:jsonData options:(|⌘|'s NSJSONReadingAllowFragments) |error|:(missing value)

thanks Stefan,
thanks for the advice, i did also notice that i can get the result by
theJSON’s |customerPhone| as text, also

yes sorry Yvan i should of included
property NSJSONSerialization : a reference to current application’s NSJSONSerialization

all the best Guys thanks for you comments

Thank’s to both of you.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 13 janvier 2019 20:59:42